1,105,288 Community Members

DataGridview - Ado.net Insert problem

Member Avatar
symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

Member Avatar
symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
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! :(

Member Avatar
cat20
Newbie Poster
1 post since Aug 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

check your database connection

Member Avatar
ddanbe
Posting Expert
5,336 posts since Oct 2008
Reputation Points: 2,054 [?]
Q&As Helped to Solve: 904 [?]
Skill Endorsements: 46 [?]
Featured
 
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:

Member Avatar
symeramon
Newbie Poster
17 posts since Jun 2011
Reputation Points: 0 [?]
Q&As Helped to Solve: 0 [?]
Skill Endorsements: 0 [?]
 
0
 

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

You
This article has been dead for over three months: Start a new discussion instead
Post:
Start New Discussion
View similar articles that have also been tagged: