Best guess - no selected item in the combo box, so it fails on this line:
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());
Check your values at the top of the method and make sure that all the user inputs are valid before you even try to go to the database. For example: is the username entered? Is it unique? Is there an email, and is it a valid format?
In addition:
1) Your connection string name should not be the same as the connection string:
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Data Source=SUMIT92\\SQLEXPRESS;Initial Catalog=Registration1.dbo;Integrated Security=True;Connect Timeout=15;Encrypt=False;TrustServerCertificate=False;ApplicationIntent=ReadWrite;MultiSubnetFailover=False"].ConnectionString);
Shoudl be somethign like:
SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString);
.
2) You should Dispose of Connection and Command objects - your code leaves the connection open if there is a problem because you don't close it in the
catch
block. Consider using a
using
block to ensure objects are always destroyed when you are finished with them:
try
{
using (SqlConnection conn= new SqlConnection (ConfigurationManager.ConnectionStrings["Registration"].ConnectionString))
{
conn.Open();
string insertQuery ="insert into Regstration2 (UserName,E-mail,Password,Country) values (@Uname,@email,@password,@country)";
using (SqlCommand com = new SqlCommand (insertQuery, conn))
{
com.Parameters.AddWithValue("@Uname", TextBoxUN.Text);
com.Parameters.AddWithValue("@email",TextBoxEM.Text);
com.Parameters.AddWithValue("@password",TextBoxp.Text);
com.Parameters.AddWithValue("@country",CN.SelectedItem.ToString());
com.ExecuteNonQuery();
Response.Write("Registration is sucessfull");
}
}
}
catch (Exception ex)
{
Response.Write("Error:"+ex.ToString());
}
3) And please, never store passwords in clear text - it is a major security risk. There is some information on how to do it here:
Password Storage: How to do it.[
^]
Also see here:
Code crime: Text based passwords[
^]