Click here to Skip to main content
15,907,392 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I want to develope an app where i can insert the gridview data in to a table .

please give me some suggestions please.
Posted
Comments
Varun Sareen 19-Mar-12 0:04am    
have you tried something? what table; a DATATABLE or dB Table
ashu1810 19-Mar-12 5:11am    
Its a database table


i used this insert statement

string statment = string.Format("insert into PROD_INVENTORY ( PROD_GRP_ID, PRODUCT_ID, IMEI_NO,INV_CAT_ID,QTY,CREATED_BY,CREATION_DATE,LAST_UPDATED_BY,LAST_UPDATE_DATE, VALID,WAREHOUSE_ID) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}' )", item.Cells[1].Text, item.Cells[2].Text," null", '1',item.Cells[3].Text,"xyz",DateTime.Now.ToString(),"xyz",DateTime.Now.ToString(),'y',ddlwarehouseid.SelectedItem.ToString());

here in place of gridview values i.e, item.cells[i].text, it is inserting 0's in these cells PROD_GRP_ID,PRODUCT_ID,QTY of PROD_INVENTORY Table.
please give me some suggestions

I did it something like this, and its working now.
C#
protected void Btnrecv_Click(object sender, EventArgs e)
  {

      callgrid();
      for (int i = 0; i < GridView1.Rows.Count; i++)
      {

          int PGID = Convert.ToInt32( ds.Tables["T"].Rows[i][1].ToString());
          int PID = Convert.ToInt32(ds.Tables["T"].Rows[i][2].ToString());
          int qty = Convert.ToInt32(ds.Tables["T"].Rows[i][3].ToString());
          int wrid = Convert.ToInt32(ddlwarehouseid.SelectedItem.ToString());
          cmd.CommandType = CommandType.StoredProcedure;
          cmd.Parameters.Clear();
          cmd.CommandText = "insert_update";
          cmd.Parameters.AddWithValue("@PGID", PGID);
          cmd.Parameters.AddWithValue("@PID", PID);
          cmd.Parameters.AddWithValue("@IMEINO", "NULL");
          cmd.Parameters.AddWithValue("@INVCAT", 1);
          cmd.Parameters.AddWithValue("@QTY", qty);
          cmd.Parameters.AddWithValue("@CDB", " LOGGEDIN USER");
          cmd.Parameters.AddWithValue("@CD", DateTime.Now.ToString());
          cmd.Parameters.AddWithValue("@LUB", "LOGGEDIN USER");
          cmd.Parameters.AddWithValue("@LUD", DateTime.Now.ToString());
          cmd.Parameters.AddWithValue("@VALID", "Y");
          cmd.Parameters.AddWithValue("@WRID", wrid);

          cmd.Connection = cn;
          cn.Open();
          cmd.ExecuteNonQuery();
          cn.Close();


and I used stored Procedure

SQL
ALTER procedure [dbo].[insert_update] (@PGID int ,@PID int,@IMEINO VARCHAR(30),@INVCAT INT,@QTY int,@CDB VARCHAR(20) ,@CD DATE,@LUB VARCHAR(20),@LUD DATE,@VALID CHAR(1),@WRID int)
AS
BEGIN
IF NOT EXISTS (SELECT * FROM PROD_INVENTORY WHERE PROD_GRP_ID=@PGID AND PRODUCT_ID=@PID AND WAREHOUSE_ID=@WRID )
INSERT INTO PROD_INVENTORY(PROD_GRP_ID,PRODUCT_ID,IMEI_NO,INV_CAT_ID,QTY,CREATED_BY,CREATION_DATE,LAST_UPDATED_BY,LAST_UPDATE_DATE,VALID,WAREHOUSE_ID)
VALUES (@PGID, @PID,@IMEINO,@INVCAT,@QTY,@CDB,@CD,@LUB,@LUD,@VALID,@WRID)

ELSE
UPDATE PROD_INVENTORY SET
QTY=QTY+@QTY,CREATED_BY=@CDB,CREATION_DATE=@CD,LAST_UPDATED_BY=@LUB,LAST_UPDATE_DATE=@LUD,VALID=@VALID WHERE PROD_GRP_ID=@PGID AND PRODUCT_ID=@PID AND WAREHOUSE_ID=@WRID
END
 
Share this answer
 
sry for being late and if this didn't work with u give me script of the table i tried this time to simulate your table hopes it help u :)

C#
protected void Page_Load(object sender, EventArgs e)
  {
      if (!IsPostBack)
      {
          binding();
      }
  }

C#
void binding()
  {
      SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=TestDB;Integrated Security=True");
      con.Open();
      SqlCommand cmd = new SqlCommand("select * from PROD_INVENTORY", con);
      cmd.CommandType = CommandType.Text;
      SqlDataAdapter adpt = new SqlDataAdapter(cmd);
      DataTable dt = new DataTable();
      adpt.Fill(dt);
      GridView1.DataSource = null;
      GridView1.DataSource = dt;
      GridView1.DataBind();
      con.Close();
      cmd.Dispose();

  }

C#
protected void Button1_Click(object sender, EventArgs e)
  {
      SqlConnection cn = new SqlConnection(@"Data Source=MOHAMED-PC\SQLEXPRESS;Initial Catalog=TestDB;Integrated Security=True");
      SqlCommand cmd = new SqlCommand();
     cn.Open();
      string statment ="";
      foreach (GridViewRow item in GridView1.Rows)
      {

       statment   = string.Format("insert into PROD_INVENTORY ( PROD_GRP_ID, PRODUCT_ID, IMEI_NO,INV_CAT_ID,QTY,CREATED_BY) values ('{0}','{1}','{2}','{3}','{4}','{5}' )", item.Cells[2].Text, item.Cells[3].Text, null, '1',
              item.Cells[4].Text.ToString(), "ashu1810");
         //  cmd = new SqlCommand(statment, cn);


      }
      cmd.CommandText = statment;
      cmd.Connection = cn;
      cmd.CommandType = CommandType.Text;
      cmd.ExecuteNonQuery();
      cn.Close();
      cmd.Dispose();
      binding();

  }
 
Share this answer
 
Comments
ashu1810 21-Mar-12 3:36am    
This did not work, still it is taking 0's in place of cells values

i am binding the gridview with a data from different tables using foreign keys

select rd.REQUEST_ID,pg.ENAME,p.ENAME,rd.QTY,rd.UNIT_PRICE,s.COMPANY_NAME,rm.TOTAL_AMOUNT,rm.EFFECTIVE_END_DATE,rm.EFFECTIVE_START_DATE,rm.DESCRIPTION,rm.STATUS from REQUESTED_PROD_DETAIL rd,REQUESTED_PROD_MST rm, PRODUCT_GROUP pg,PRODUCTS p,SUPPLIERS s where rd.PROD_GRP_ID=pg.PROD_GRP_ID and rd.PROD_GRP_ID=p.PROD_GRP_ID and rd.REQUEST_ID= rm.REQUEST_ID and rm.SUPPLIER_ID=s.SUPPLIER_ID and rd.REQUEST_ID='" + id + "

and after getting the data into the grid
I want them to insert into one table called PRODUCT_INVENTORY

while displaying in the grid i should display the PRODUCT GROUP NAME(ENAME), PRODUCTNAME(ENAME),SUPPIER NAME(COMPANY NAME), these fields are form different tables like PRODUCT_GROUP, PRODUCTS, SUPPLIERS.

but while inserting data from grid to other table that is PROD_INVENTORY Table i should insert not the names but the corresponding id like PROD_GRP_ID,PRODUCT_ID,and SUPPLIER_ID and some other values.

which table script you are telling about I am not getting.
there are 6 tables, all together.

is there any other way I can do this same, I tried a lot but I didnt get it
I am new to this.please gv me some advice.
please help me.
hi ,


C#
protected void Page_Load(object sender, EventArgs e)
   {
       if (!IsPostBack)
       {
           SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
           con.Open();
           SqlCommand cmd = new SqlCommand("select * from TestTb", con);
           cmd.CommandType = CommandType.Text;
           SqlDataAdapter adpt = new SqlDataAdapter(cmd);
           DataTable dt = new DataTable();
           adpt.Fill(dt);
           GridView1.DataSource = dt;
           GridView1.DataBind();
       }
   }


C#
private void button1_Click(object sender, EventArgs e)
       {
           foreach (DataGridViewRow item in dataGridView1.Rows)
           {


               SqlConnection con = new SqlConnection(@"Data Source=.;Initial Catalog=test;Integrated Security=True");
               con.Open();
               string statment = string.Format("insert into  test1 ( [name], [address], gender ) values ('{0}','{1}','{2}' )", item.Cells[1].Value.ToString(), item.Cells[2].Value.ToString(), item.Cells[3].Value.ToString());
               SqlCommand cmd = new SqlCommand(statment, con);

               cmd.CommandType = CommandType.Text;
               cmd.ExecuteNonQuery();
               con.Close();
               cmd.Dispose();

           }
       }
 
Share this answer
 
Comments
ashu1810 20-Mar-12 5:13am    
I tried this code.
and when I traced the code I found that items.cell[].text.tostring() is holding the null values(""), and the 0's are getting inserted into the table.
and I didnt find the "Value" property so I just used the Text property.
Is there anything wrong in the code please give me advice.
protected void btnview0_Click(object sender, EventArgs e)
{

callgrid();

}
protected void Btnrecv_Click(object sender, EventArgs e)
{
foreach (GridViewRow item in GridView1.Rows)
{

cn.Open();


string statment = string.Format("insert into PROD_INVENTORY ( PROD_GRP_ID, PRODUCT_ID, IMEI_NO,INV_CAT_ID,QTY,CREATED_BY,CREATION_DATE,LAST_UPDATED_BY,LAST_UPDATE_DATE, VALID,WAREHOUSE_ID) values ('{0}','{1}','{2}','{3}','{4}','{5}','{6}','{7}','{8}','{9}','{10}' )", item.Cells[2].Text.ToString(), item.Cells[3].Text.ToString(), " null", '1', item.Cells[4].Text.ToString(), "+xyz+", DateTime.Now.ToString(), "xyz", DateTime.Now.ToString(), 'y', ddlwarehouseid.SelectedItem.ToString());
SqlCommand cmd = new SqlCommand(statment, cn);

cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
cn.Close();
cmd.Dispose();

}
public void callgrid()
{
string id = ddlreqid.SelectedItem.ToString();

da = new SqlDataAdapter(" select rd.REQUEST_ID,rd.PROD_GRP_ID,rd.PRODUCT_ID,rd.QTY,rd.UNIT_PRICE,rm.SUPPLIER_ID,rm.TOTAL_AMOUNT,rm.EFFECTIVE_END_DATE,rm.EFFECTIVE_START_DATE,rm.DESCRIPTION,rm.STATUS from REQUESTED_PROD_DETAIL rd,REQUESTED_PROD_MST rm where rd.REQUEST_ID= rm.REQUEST_ID and rd.REQUEST_ID='" + id + "'", cn);
DataTable dt = new DataTable();

da.Fill(dt);

GridView1.DataSource = dt;
GridView1.DataBind();

}

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900