hi....

I have a gridview with rows of info which does not go into edit mode.

A user clicks a checkbox which picks that item and adds a Unit Price

What i need to do is when a user clicks on a button, it should scan through the gridview for all items checked and pick up the Unit Price values and a couple of other field values and insert the information into a sql database

What would be the best way of doing this?

Thanks in advance!

Recommended Answers

All 5 Replies

on button click event u jusst check what are the checkboxexs clicked.
u can use this function

submit_click
{
for(int i=0;i<=gridview.rows.count-1;i++)
{
checkbox chk=new checkbox();
chk=(checkbox)gridview.rows[i].findcontrol("chk_name");
if(chk.checked)
{
//write the code needed to get unit prices and insert into database
}

}
}

on button click event u jusst check what are the checkboxexs clicked.
u can use this function

submit_click
{
for(int i=0;i<=gridview.rows.count-1;i++)
{
checkbox chk=new checkbox();
chk=(checkbox)gridview.rows.findcontrol("chk_name");
if(chk.checked)
{
write the code needed to get unit prices and insert into database
}

}
}

hello thanks for the quick reply...
my problem is how to wirte an SQL statement to insert multiple records.. can you please give me an idea or the code for inserting checked records. i dont konw what code to put in this line
write the code needed to get unit prices and insert into database
}

}

can u send us u r code.It will help to understand u r query better.

protected void Button2_Click(object sender, EventArgs e)
    {
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ToString());

        for (int i = 0; i < GridView1.Rows.Count; i++)
        {
            GridViewRow row = GridView1.Rows[i];
            CheckBox chkbox = (CheckBox)row.FindControl("chkboxSelect");
            TextBox UnitPrice = (TextBox)row.FindControl("txtUnitPrice");

            if (chkbox.Checked == true)
            {
                try
                {
                    conn.Open();

                    int Supp = Convert.ToInt16(Request.QueryString["SupplierId"]);
                    SqlCommand cmd = new SqlCommand ("INSERT INTO SuppMat (SupplierId, MaterialID, UnitPrice) VALUES (@SupplierId,'" + row.Cells[1].Text + "','" + UnitPrice.Text + "')", conn);
                    cmd.Parameters.AddWithValue("@SupplierId", Supp);
                   
                    cmd.ExecuteNonQuery();
                    conn.Close();
                }
                catch
                {
                    Response.Write("");
                }
                finally
                {
                    conn.Close();
                }
            }
           
        }
        
    }

here's my code..my only problem now is that it does not save to the database.. hope you can help me..

r u getting values in row.cells.text.did u test u r query in query analyser ,r there any problems.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.