c# - Can't solve the "An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code" -


i have form in can enter email , receives instructions on resetting password. has textbox (txtemail), submit button (btnresetpassword) , lblmessage.

my c# code looks this:

protected void btnresetpassword_click(object sender, eventargs e) {     string cs = configurationmanager.connectionstrings["defaultconnection"].connectionstring;      using (sqlconnection con = new sqlconnection(cs))     {         sqlcommand cmd = new sqlcommand("spresetpassword", con);         cmd.commandtype = commandtype.storedprocedure;          sqlparameter paramemail = new sqlparameter("@email", txtemail.text);         cmd.parameters.add(paramemail);          con.open();          sqldatareader rdr = cmd.executereader();          while (rdr.read())         {             if (convert.toboolean(rdr["returncode"]))             {                 sendpasswordresetemail(rdr["email"].tostring(), rdr["uniqueid"].tostring());                 lblmessage.text = "an email instructions reset password sent registered email";             }             else             {                 lblmessage.forecolor = system.drawing.color.red;                 lblmessage.text = "username not found!";             }         }     } }  

i have method sendpasswordresetemail(string email, string id) works fine (smtp).

the stored procedure is:

create proc spresetpassword @email nvarchar(100)   begin      declare @userid int       select @userid = id      dbo.turiststbl      email = @email       if (@userid not null)      begin          --if username exists          declare @guid uniqueidentifier           set @guid = newid()           insert tblresetpasswordrequests (id, userid, resetrequestdatetime)         values (@guid, @userid, getdate())           select              1 returncode,              @guid uniqueid,              @email email      end      else      begin          --if username not exist          select              0 returncode,              null uniqueid,              null email      end end 

when enter email, following error:

see screenshot here

what can do?
edit: database files not local, on remote server

the problem origins connection string inside web.config file:

<connectionstrings>      <add name="defaultconnection" connectionstring="data source=(localdb)\mssqllocaldb;     attachdbfilename=|datadirector‌​y|\aspnet-vizito-201‌​70305102044.mdf;     init‌​ial catalog=aspnet-vizito-20170305102044;     integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> 

here you're attempting use localdb instance mentioning attachdbfilename parameter on connecting string attribute, , shouldn't use initial catalog parameter on same string.

hence, removing initial catalog part, connection string should this:

<connectionstrings>      <add name="defaultconnection" connectionstring="data source=(localdb)\mssqllocaldb;     attachdbfilename=|datadirector‌​y|\aspnet-vizito-201‌​70305102044.mdf;     integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> 

additionally, if database file placed outside of app_data directory inside project, may require full file path refer location this:

<connectionstrings>      <add name="defaultconnection" connectionstring="data source=(localdb)\mssqllocaldb;     attachdbfilename=c:\users\hp-pc\desktop\vizitositemaster\vizito\app_data\aspn‌​et-vizito-2017030510‌​2044.mdf;     integrated security=true" providername="system.data.sqlclient" /> </connectionstrings> 

update:

according op explaining db takes place on remote server instance, connection string should use initial catalog this:

<connectionstrings>      <add name="defaultconnection" connectionstring="data source=[server address];     init‌​ial catalog=aspnet-vizito-20170305102044; user id=[user id]; password=[password];      integrated security=sspi" providername="system.data.sqlclient" /> </connectionstrings> 

nb: replace [server address] remote server ip address (with port if exists), [user id] & [password] user credentials used on remote server.

references:

sql server connection string

cannot attach file *.mdf database

an attempt attach auto-named database file ....database1.mdf failed


Comments