Hi,
If i am applying the above code.it will jump to the else part.if i execute the command like "insert into tbl_product (productname)values('" + ProductName + "')".it shows the result as one row affected.in my project debugging it will goes to the else part.shows the message as product not added.what the problem in my code.
private void btn_addproduct_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("data source=mars-104;initial catalog=marsweb;integrated security=true");
string sql = "insert into tbl_product (productname)values('" + ProductName + "')";
SqlCommand cmd = new SqlCommand(sql, con);
//SqlCommand cmd = new SqlCommand("insert into tbl_product(productname)values(@productname)",con );
cmd.Parameters.AddWithValue("@productname", txt_product.Text);
SqlDataAdapter da = new SqlDataAdapter(cmd);
con.Open();
DataTable dt = new DataTable();
da.Fill(dt);
if (dt.Rows.Count > 0)
{
MessageBox.Show("product added successfully..");//it wont execute the if part it goes to else part directly
txt_product.Text = "";
}
else
{
MessageBox.Show("product not added");
txt_product.Text = "";
}
}
Best,
Sudheep.
Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator