Hello everyone. I'm working on an application and I keep getting this exception when i run the code listed below. I'm new here. Please help.
My connection string in My Global.asax file:
Application["connstr"] = "data source=SERVER\\VSDOTNET;initial catalog=MedicalCenterDB;integrated security=SSPI;persis" +
"t security info=False;workstation id=SERVER";
private void submitbtn_Click(object sender, System.Web.UI.ImageClickEventArgs e)
{
System.Data.SqlClient.SqlConnection cn;
System.Data.SqlClient.SqlConnection cn2;
System.Data.SqlClient.SqlCommand cmd;
System.Data.SqlClient.SqlCommand cmd2;
System.Data.SqlClient.SqlDataReader dr;
cn = new System.Data.SqlClient.SqlConnection();
cn2 = new System.Data.SqlClient.SqlConnection();
cn.ConnectionString = Application["connstr"].ToString();
cn2.ConnectionString = Application["connstr"].ToString();
cmd = new System.Data.SqlClient.SqlCommand("selectUserfromCard",cn);
cmd2 = new System.Data.SqlClient.SqlCommand("saveUser",cn2);
cmd.CommandType=CommandType.StoredProcedure;
cmd2.CommandType=CommandType.StoredProcedure;
try
{
cn.Open();
cmd.Parameters.Add("@idcard",idcard.Text);
//
//cmd2
//
dr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
if(dr.Read())
{
resultlbl.Text = "A User with this Card already exists";
}
else
{
string dob = this.bday.SelectedItem.Value.ToString() + "/" + this.bmonth.SelectedItem.Value.ToString() + "/" + this.byear.SelectedItem.Value.ToString();
cmd2.Parameters.Add("@idcard",idcard.Text);
cmd2.Parameters.Add("@Password",password.Value);
cmd2.Parameters.Add("@Firstname",Firstname.Text);
cmd2.Parameters.Add("@Lastname",Lastname.Text);
cmd2.Parameters.Add("@Othername",Othername.Text);
cmd2.Parameters.Add("@Sex",this.sex.SelectedItem.Value.ToString());
cmd2.Parameters.Add("@Email",email.Text);
cmd2.Parameters.Add("@Nationality",Nationality.Text);
cmd2.Parameters.Add("@State",State.Text);
cmd2.Parameters.Add("@Tribe",Tribe.Text);
cmd2.Parameters.Add("@Occupation",Occupation.Text);
cmd2.Parameters.Add("@dateofbirth",dob);
cn2.Open();
cmd2.ExecuteNonQuery();
resultlbl.Text = "Info Saved";
}
}
catch(System.Exception eLoad)
{
resultlbl.Text = eLoad.Message;
}
finally
{
cn.Close();
cn2.Close();
}
}
Thanks a lot.