prabha haran

prabha haran

  • NA
  • 81
  • 32.1k

when inserting records from grid to database first row inser

Mar 25 2017 6:52 AM
hi
 when i insert records from grid to database only first row is getting inserted to the count of grid and my code is
 
foreach (GridViewRow g1 in SalesInvoiceGridView.Rows)
{

string pcode = SalesInvoiceGridView.Rows[i].Cells[3].Text;
DataSet dp = da.GetDataSet( "select ProductId from Tbl_Product where ProductCode ='" + pcode.ToString() + "'");
var Gender = dp.Tables[0].Rows[0][0].ToString();
int prodid = Convert.ToInt32(Gender);
DataSet dp1 = da.GetDataSet("select Barcode from Tbl_Barcodes where Product_Id ='" + prodid + "'");
var bcode = dp1.Tables[0].Rows[0][0].ToString();
DataSet dp2 = da.GetDataSet("select Price from Tbl_Product where ProductId ='" + prodid + "'");
var pric = dp2.Tables[0].Rows[0][0].ToString();
decimal pricp = Convert.ToDecimal(pric);
DataSet dp3 = da.GetDataSet("select UnitId from Tbl_Product where ProductId ='" + prodid + "'");
var uid = dp3.Tables[0].Rows[0][0].ToString();
int u1id = Convert.ToInt16(uid);
DataSet dp4 = da.GetDataSet("select UnitName from Tbl_UOM where UnitId ='" + u1id + "'");
var uname = dp4.Tables[0].Rows[0][0].ToString();
string u1name = uname;

//var proid = da.ExecuteCmd(query);
//DataSet ds = da.GetDataSet("select ProductId from Tbl_Product where ProductCode ='" + pcode.ToString() + "'");
//if (ds.Tables[0].Rows.Count > 0)
//{

// //txtClass.Text = ds.Tables[0].Rows[0][0].ToString();
// string Gender = ds.Tables[0].Rows[0][0].ToString();
//ds = ObjectAccess.GetDataSet(query);
//var proid = ds.Tables[0];
//int customerId = int.Parse(SalesInvoiceGridView.Rows.Cells[0].Text);
//SqlCommand cmd = new SqlCommand();
//SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["strConnection"].ConnectionString);
//SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=ID_Proof;Integrated Security=True");
cmd.CommandText = ("INSERT INTO Tbl_StockTransferProducts (STId,ProductId,Quantity,Amount,NetAmount,DiscountAmt,ItemCode,UOMId,Barcode,Price) values (@STId,@ProductId,@qty,@Amount,@NetAmount,@DiscountAmt,@ItemCode,@UOMId,@Barcode,@Price)");
//cmd.Parameters.AddWithValue("@STId", SalesInvoiceGridView.Rows[i].Cells[0].ToString());
cmd.Parameters.AddWithValue("@qty", Convert.ToDecimal(SalesInvoiceGridView.Rows[i].Cells[6].Text));
//cmd.Parameters.AddWithValue("@Rights", SalesInvoiceGridView.Rows[i].Cells[2].ToString());
cmd.Parameters.AddWithValue("@Amount", Convert.ToDecimal(SalesInvoiceGridView.Rows[i].Cells[5].Text));
cmd.Parameters.AddWithValue("@NetAmount", Convert.ToDecimal(SalesInvoiceGridView.Rows[i].Cells[7].Text));
cmd.Parameters.AddWithValue("@DiscountAmt", Convert.ToDecimal(SalesInvoiceGridView.Rows[i].Cells[9].Text));
//cmd.Parameters.AddWithValue("@Tax", SalesInvoiceGridView.Rows[i].Cells[6].ToString());
cmd.Parameters.AddWithValue("@ItemCode", SalesInvoiceGridView.Rows[i].Cells[3].Text);
cmd.Parameters.AddWithValue("@UOMId", u1id);
//cmd.Parameters.AddWithValue("@Barcode", SalesInvoiceGridView.Rows[i].Cells[9].ToString());
cmd.Parameters.AddWithValue("@ProductId", prodid);
cmd.Parameters.AddWithValue("@STId", insertedID);
cmd.Parameters.AddWithValue("@Barcode", bcode);
cmd.Parameters.AddWithValue("@Price", pricp);


con.Open();

//cmd.ExecuteScalar();
cmd.ExecuteNonQuery();

con.Close();

cmd.Parameters.Clear();

Answers (4)