Click here to Skip to main content
15,878,809 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
i have a database from which i am able to select but not able to update, insert data in data base
here is my code:
C#
string StrQuery;
            try
            {
                using (SqlConnection conn = new SqlConnection(@"Data Source=(LocalDB)\v11.0;AttachDbFilename=|DataDirectory|\Demo_Data.mdf;Integrated Security=True;Connect Timeout=30"))
                {
                    using (SqlCommand comm = new SqlCommand())
                    {
                        comm.Connection = conn;
                        conn.Open();

                        for (int i = 0; i < dataGridView1.Rows.Count; i++)
                        {
                            StrQuery = @"INSERT INTO umrah (S#No#, Name, Code, Area, Sector, CNIC, TotalCoupen) VALUES ('"
                                
                                + dataGridView1.Rows[i].Cells["S#No#"].Value + "','"
                                + dataGridView1.Rows[i].Cells["Name"].Value + "','"
                                + dataGridView1.Rows[i].Cells["Code"].Value + "','"
                                + dataGridView1.Rows[i].Cells["Area"].Value + "','"
                                + dataGridView1.Rows[i].Cells["Sector"].Value + "','"
                                + dataGridView1.Rows[i].Cells["CNIC"].Value + "','"
                                + dataGridView1.Rows[i].Cells["TotalCoupen"].Value + "');";
                            comm.CommandText = StrQuery;
                            comm.ExecuteNonQuery();
                        }
                    }
                }
            }
            catch { }
Posted
Comments
[no name] 18-Dec-14 15:26pm    
1.) You will get soon some comments that you should use parameterized queries to avoid SQL injection
2.) You do not "UPDATE" you "INSERT"
3.) What is the Error Message which will displayed?
Maciej Los 18-Dec-14 15:37pm    
This is an answer, Bruno ;)
[no name] 18-Dec-14 15:51pm    
Not yet ;)
And I think I will/can not answer. Not because I don't like it is more because I'm not safe enough with "this new" technic. "UPDATING" from a grid in this way looks horrible for me.
I lean back and wait to read your answer :-)
Bruno
George Jonsson 18-Dec-14 23:34pm    
I agree. The OP should use an underlying data table.
Maciej Los 18-Dec-14 16:22pm    
What error message?

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