Hi there all, I am trying to get data from a sql view and bound it into the datagridview, after that, I add 4 more colums into the datagridview which are editable, finally pass the entered data into a new database and call that data in the future in the load of Datagridview... I have 19 colums return from view, 4 more colums I have added. Here is the code;

string[] kolon = { "Asd1", "Asd2", "Asd3", "Asd4" };
        private void button1_Click(object sender, EventArgs e)
        {
            string ad;
            SqlConnection conn = new SqlConnection("Server=.;Database=CrmDatabase;Trusted_Connection=True");
            for (int i = 0; i < dataGridView1.Rows.Count; i++)
            {
                for (int j = 19 , h = 0; j < dataGridView1.Columns.Count; j++, h++)
                {
                    if (!(dataGridView1.Rows[i].Cells[j].Value == null))
                    {
                        ad = dataGridView1.Rows[i].Cells[j].Value.ToString();
                        if (conn.State == System.Data.ConnectionState.Closed)
                            conn.Open();
                        SqlCommand komut = new SqlCommand("Insert into Raporlama(" + kolon[h] + ") values(" + ad + ")", conn);
                        listBox1.Items.Add("Insert into Raporlama(" + kolon[h] + ") values(" + ad + ")");
                        if (conn.State == System.Data.ConnectionState.Open)
                            conn.Close();
                    }
                }
            }
            MessageBox.Show("kayıt tamam");
        }

The problem is that no errors in C# but no data entered into Raporlama table, even no reach I suggest... Please send your precious helps... :(

Recommended Answers

All 5 Replies

So, no one knows??? Wow! :D

I have just implemented a little change,

SqlCommand komut = new SqlCommand("Insert into Raporlama(" + kolon[h] + ")" + " " +  "values(" + "'" + ad + "'" + ")", conn);

                        listBox1.Items.Add("Insert into Raporlama(" + kolon[h] + ")" + " " + "values(" + "'" + ad + "'" + ")");

But still the same! :(

check your database connection

Think line 8 of your first code post never gets executed.
j=19 and so is Columns.Count so the condition in youfor statement: j < dataGridView1.Columns.Count; will end the for loop.
BTW. It is a bad habit to use hardcoded numbers in your code.
But I confess, sometimes I still have bad habits too.:icon_evil:

Thanks ddanbe... I found its problem but I cant remember how did I solve that now! :) It was something like vvvVVVVİİİUUWWWWWvvv! :D

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.