Click here to Skip to main content
16,006,578 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,

i need to add values in my Grid-View at Run time and save it to the database.i tried various things like by creating a function and calling and inserting values but at run-time this Does not work. i am novice in this field and Don't know much about this ..please help.

What I have tried:

public void Fill_grid()
     {
         SqlCommand cmd = con.CreateCommand();
         cmd.CommandType = CommandType.Text;
        cmd.CommandText = "select * from DBTable";
         cmd.ExecuteNonQuery();
         DataTable dt = new DataTable();
         SqlDataAdapter da = new SqlDataAdapter(cmd);
         da.Fill(dt);
         dataGridView1.DataSource = dt;

     }

     private void dataGridView1_CellValueChanged(object sender, DataGridViewCellEventArgs e)
     {
         id = dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString();
         if (id == "")
         {
             id1 = 0;
         }
         else
         {
             id1 = Convert.ToInt32(dataGridView1.Rows[e.RowIndex].Cells["id"].Value.ToString());
         }

         if (id1 == 0)
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "Insert into DBTable values('" + dataGridView1.Rows[e.RowIndex].Cells["FileName"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "','" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "')";
             cmd.ExecuteNonQuery();
             Fill_grid();
         }

         else
         {
             SqlCommand cmd = con.CreateCommand();
             cmd.CommandType = CommandType.Text;
             cmd.CommandText = "update DBTable set Filename='" + dataGridView1.Rows[e.RowIndex].Cells["Filename"].Value.ToString() + "',Sequence='" + dataGridView1.Rows[e.RowIndex].Cells["Sequence"].Value.ToString() + "',Delay='" + dataGridView1.Rows[e.RowIndex].Cells["Delay"].Value.ToString() + "' where id=" + id1 + "";
             cmd.ExecuteNonQuery();
             Fill_grid();

         }

     }
Posted
Updated 2-Feb-17 22:19pm
v2

1 solution

"Does not work" is not a helpful error report - it tells us absolutely nothing about what is happening.

Start by fixing your code: Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead.

After that, a lot of this is going to be down to your data and user input - and we have no access to either of those!
So, its going to be up to you.
Put a breakpoint on the first line in the dataGridView1_CellValueChanged function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
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