Click here to Skip to main content
15,896,726 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i want to save 5 customer details(name,contact no,email id,pan no,landline no,company name,designation,etc.) at same time in sql database.
which control i should use
can i save this entries using 'datagrid view' or any other tool.
i don't want to use many textboxes on the form.
plz help me
thankyou............
Posted
Comments
Shubham Choudhary 16-Mar-13 3:22am    
hi!! hi if you don't want to use textboxes so it is good idea to use datagridview for this

1 solution

Hi if you want to enter data in datagridview after that you want to save the data you can use this code. You should change the value according to you need.
C#
void SaveEach()
        {
            try
            {
                if (dataGridView1.Rows.Count > 1)
                {
                    for (int i = 0; i <= dataGridView1.Rows.Count - 1; i++)
                    {
                        string col1 = dataGridView1.Rows[i].Cells[0].Value.ToString();
                        string col2 = dataGridView1.Rows[i].Cells[1].Value.ToString();
                        string col3 = dataGridView1.Rows[i].Cells[2].Value.ToString();
                        string col4 = dataGridView1.Rows[i].Cells[3].Value.ToString();
                        string col5 = dataGridView1.Rows[i].Cells[4].Value.ToString();
                        using (SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["constr"].ConnectionString))
                        {
                            string insert = "INSERT INTO InvoiceItems(InvoiceNo,SNo,Quantity,Rate,Description,Total) VALUES (@InvoiceNo,@SNo,@Quantity,@Rate,@Description,@Total)";
                            con.Open();
                            SqlCommand cmd = new SqlCommand(insert, con);
                            cmd.Parameters.AddWithValue("@InvoiceNo", txt_invoiceno.Text);
                            cmd.Parameters.AddWithValue("@SNo", col1.ToString());
                            cmd.Parameters.AddWithValue("Description", col2.ToString());
                            cmd.Parameters.AddWithValue("@Quantity", col3.ToString());
                            cmd.Parameters.AddWithValue("@Rate", col4.ToString());
                            cmd.Parameters.AddWithValue("Total", col5.ToString());
                            cmd.ExecuteNonQuery();
                            con.Close();
                        }
                    }
                }
            }
            catch (Exception ex)
            {
                ex.Message.ToString();
            }
        }



Thanks
 
Share this answer
 

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