Sakshi Kaul

Sakshi Kaul

  • NA
  • 72
  • 7.3k

Problem in connection string

Mar 29 2019 2:12 AM
Hi 
I am using the following code in asp.net (c# form designing)
but I am getting an error with the following line in a code :-
string EmpId= "select count(*) from RegistrationTable where EmpId ='" + TextBox1.Text + "'";
error is stored procedure EmpId could not be found
Need help to resolve the issue. 
 
protected void Page_Load(object sender, EventArgs e)
{
if(IsPostBack)
{
SqlConnection con = new SqlConnection(@"Data Source=SAKSHIKAUL-LT\SQLEXORESS1;Initial Catalog=registrationForm;Integrated Security=True");
con.Open();
string EmpId= "select count(*) from RegistrationTable where EmpId ='" + TextBox1.Text + "'";
SqlCommand cmd = new SqlCommand("EmpId", con);
int temp = Convert.ToInt32(cmd.ExecuteScalar().ToString());
if (temp==1)
{
Response.Write("employee already exists ");
}
con.Close();
}
}
protected void Button1_Click(object sender, EventArgs e)
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["RegiConnectionString"].ConnectionString);
con.Open();
string insertQuery = "insert into Ucbreg(EmpId,Nationality,Religion,Gender,Address,EmailId,Maritalstatus)values (@EmpId,@Nationality,@Religion,@Gender,@Address,@EmailId,@Maritalstatus)";
SqlCommand cmd = new SqlCommand(insertQuery, con);
cmd.Parameters.AddWithValue("@EmpId", TextBox1.Text);
cmd.Parameters.AddWithValue("@Nationality", TextBox2.Text);
cmd.Parameters.AddWithValue("@Religion", TextBox3.Text);
cmd.Parameters.AddWithValue("@Gender", TextBox4.Text);
cmd.Parameters.AddWithValue("@Address", TextBox5.Text);
cmd.Parameters.AddWithValue("@EmailId", TextBox6.Text);
cmd.Parameters.AddWithValue("@MaritalStatus", TextBox7.Text);
cmd.ExecuteNonQuery();
Response.Write("Employee registeration Successfully");
con.Close();
}
catch (Exception ex)
{
Response.Write("error" + ex.ToString());
}
}
}
 

Answers (4)