Monday, 16 January 2012

selecting Data from database for checking of duplication and then inserting into table in C#

SqlConnection scon = new SqlConnection("Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True");
        SqlCommand scmd = new SqlCommand("select PO from PO_Table Where PO=@PO", scon);
        scmd.Parameters.Add("PO", SqlDbType.Int).Value=TextBox1.Text;
        SqlDataAdapter sda = new SqlDataAdapter(scmd);
        DataSet ds = new DataSet();
        sda.Fill(ds);

       
        if (TextBox1.Text == ds.Tables[0].Rows[0][0].ToString())
        {


            Response.Write("PO Already Exists");

        }

        else
        {
            SqlConnection use = new SqlConnection(" Data Source=FAISALN;Initial Catalog=Shipping;Integrated Security=True ");
            SqlDataAdapter da = new SqlDataAdapter();
            da.InsertCommand = new SqlCommand("insert into PO_Table values (@PO, @Style_Ref, @Price, @Season, @Order_Qty, @PO_Ship_Date, @PO_Shipping_Month, @PO_Shipping_Mode, @PO_Value, @PO_Destination)", use);
            da.InsertCommand.Parameters.Add("@PO", SqlDbType.Int).Value = TextBox1.Text;
            da.InsertCommand.Parameters.Add("@Style_Ref", SqlDbType.NVarChar).Value = TextBox2.Text;
            da.InsertCommand.Parameters.Add("@PO_Destination", SqlDbType.NVarChar).Value = DropDownList3.Text;
            da.InsertCommand.Parameters.Add("@Price", SqlDbType.Money).Value = TextBox3.Text;
            da.InsertCommand.Parameters.Add("@Season", SqlDbType.NVarChar).Value = DropDownList2.Text;
            da.InsertCommand.Parameters.Add("@Order_Qty", SqlDbType.Int).Value = TextBox4.Text;
            da.InsertCommand.Parameters.Add("@PO_Ship_Date", SqlDbType.DateTime).Value = TextBox5.Text;
            da.InsertCommand.Parameters.Add("@PO_Shipping_Month", SqlDbType.Char).Value = TextBox6.Text;
            da.InsertCommand.Parameters.Add("@PO_Shipping_Mode", SqlDbType.Char).Value = TextBox7.Text;
            da.InsertCommand.Parameters.Add("@PO_Value", SqlDbType.Int).Value = TextBox8.Text;
            use.Open();
            da.InsertCommand.ExecuteNonQuery();
            use.Close();
            TextBox1.Text = "";
            TextBox2.Text = "";
            TextBox3.Text = "";
            TextBox4.Text = "";
            TextBox5.Text = "";
            TextBox6.Text = "";
            TextBox7.Text = "";
            TextBox8.Text = "";

        }

        scon.Close();

No comments:

Post a Comment