954,510 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

DataGridview - Ado.net Insert problem

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... :(

symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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

symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

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! :(

symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

check your database connection

cat20
Newbie Poster
1 post since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

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:

ddanbe
Senior Poster
3,829 posts since Oct 2008
Reputation Points: 2,070
Solved Threads: 661
 

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

symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 10
Solved Threads: 0
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: