how can i insert the records into gridview?

Posted by Sudheep.grandhe under C# on 12/13/2011 | Points: 10 | Views : 967 | Status : [Member] | Replies : 1
Hi,

how can i insert the records into gridview?

public void insertreport2data()
{

DBConnection db = new DBConnection();
SqlConnection con = db.opencon();
SqlCommand cmd = new SqlCommand("insert into tbl_report2 (compname,compaddress,invoiceno,orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount)values(@compname,@compaddress,@invoiceno,@orderno,@ondate,@invoicedate,@amountstatus,@netamount,@netvat,@tamount)", con);
SqlCommand cmd2 = new SqlCommand("insert into tbl_report1 (particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid) select particulars,quantity,perprice,amount,orderno,compname,invoicedate,ondate,invoiceid from tbl_temp", con);
cmd.Parameters.AddWithValue("@compname", cmb_companyname.SelectedItem.ToString());
cmd.Parameters.AddWithValue("@compaddress", txt_customeraddress.Text);
cmd.Parameters.AddWithValue("@invoiceno", txt_invoiceno.Text);
cmd.Parameters.AddWithValue("@orderno", txt_orderno.Text);
if (txt_invoicedate.Text != null && txt_invoicedate.Text.Trim() != "")
{
cmd.Parameters.AddWithValue("@invoicedate", txt_invoicedate.Text);
}
else
{
cmd.Parameters.AddWithValue("@invoicedate", DBNull.Value);
}
if (txt_ondate.Text != null && txt_ondate.Text != "")
{
cmd.Parameters.AddWithValue("@ondate", txt_ondate.Text);
}
else
{
cmd.Parameters.AddWithValue("@ondate", DBNull.Value);
}

cmd.Parameters.AddWithValue("@amountstatus", rb_paid.Checked);
//cmd.Parameters.AddWithValue("@amountstatus", rb_unpaid.Checked);
cmd.Parameters.AddWithValue("@netamount", txt_netamount.Text);
cmd.Parameters.AddWithValue("@netvat", txt_vat.Text);
cmd.Parameters.AddWithValue("@tamount", txt_totalamount.Text);
cmd.Parameters.AddWithValue("@invoiceid",txt_invoiceno .Text );
SqlDataAdapter da2 = new SqlDataAdapter(cmd2);
SqlDataAdapter da = new SqlDataAdapter(cmd);
if (con.State == ConnectionState.Closed)
{
con.Open();
}
cmd.ExecuteNonQuery();
cmd2.ExecuteNonQuery();
con.Close();

}

Best,
Sudheep.



Responses

Posted by: Sksingh on: 12/13/2011 [Member] Starter | Points: 25

Up
0
Down
Hi,

Replace

SqlCommand cmd = new SqlCommand("insert into tbl_report2 (compname,compaddress,invoiceno,orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount)values(@compname,@compaddress,@invoiceno,@orderno,@ondate,@invoicedate,@amountstatus,@netamount,@netvat,@tamount)", con);

with

SqlCommand cmd = new SqlCommand(" SET IDENTITY_INSERT tbl_report2 ON
GO insert into tbl_report2 (compname,compaddress,invoiceno,orderno,ondate,invoicedate,amountstatus,netamount,netvat,tamount)values(@compname,@compaddress,@invoiceno,@orderno,@ondate,@invoicedate,@amountstatus,@netamount,@netvat,@tamount) GO SET IDENTITY_INSERT tbl_report2 OFF
", con);


Let give a try to check whether there is identity column or not in tbl_report2 table

Regards,
Sunil

Sudheep.grandhe, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response