Write SQL INSERT Query with WHERE Clause

Last Reply on Dec 08, 2013 03:07 AM By Mudassar

Posted on Dec 08, 2013 01:18 AM
protected void gvDetails_RowCommand1(object sender, GridViewCommandEventArgs e)
    {
        if (e.CommandName.Equals("AddNew"))
        {
           // TextBox txtsno = (TextBox)gvDetails.FooterRow.FindControl("txtsno");
            TextBox txtchitti = (TextBox)gvDetails.FooterRow.FindControl("txtchitti");
            con.Open();

                        
            
            
            SqlCommand cmd = new SqlCommand("insert into chitti(Chitti) values(" + txtchitti.Text + ")", con);
            int result = cmd.ExecuteNonQuery();
            con.Close();
            if (result == 1)
            {
                BindMasteritemDetails();
                lblresult.Text = txtchitti.Text + " Details inserted successfully";
            }
            else
            {
                
                lblresult.Text = txtchitti.Text + " Details not inserted";
            }


        }

    }

i want to do only one time insertation of data , but this code inserting every time.i used 

 SqlCommand cmd = new SqlCommand("insert into chitti(Chitti) values(" + txtchitti.Text + " where SerialNo<2)", con); but it is not working..
Posted on Dec 08, 2013 01:19 AM

Insert does not work with where, you must use Update Instead


Posted on Dec 08, 2013 03:07 AM

You need to check in database

IF NOT EXISTS(SELECT City FROM Table WHERE City = 'Mumbai')
BEGIN
    --INSERT HERE
END