Click here to Skip to main content
15,887,280 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
NpgsqlCommand cd = new NpgsqlCommand("insert into client (ref_client,type_client,nom,adresse,tele,fax) values (@ref_client,@type_client,@nom,@adresse,@tele,@fax) ", cnx);
            Console.WriteLine(dataGridView1.SelectedRows.Count.ToString());
            MessageBox.Show(dataGridView1.SelectedRows.Count.ToString());

            cd.Parameters.AddWithValue("@ref_client", dataGridView1.SelectedRows[2].Cells[1].Value.ToString());
            cd.Parameters.AddWithValue("@type_client", dataGridView1.SelectedRows[2].Cells[2].Value.ToString());
            cd.Parameters.AddWithValue("@nom", dataGridView1.SelectedRows[2].Cells[3].Value.ToString());
            cd.Parameters.AddWithValue("@adresse", dataGridView1.SelectedRows[2].Cells[4].Value.ToString());
            cd.Parameters.AddWithValue("@tele", dataGridView1.SelectedRows[2].Cells[5].Value.ToString());
            cd.Parameters.AddWithValue("@fax", dataGridView1.SelectedRows[2].Cells[6].Value.ToString());
            cnx.Open();
            cd.ExecuteNonQuery();
            cnx.Close();
C#



What I have tried:

can i get help about my code .
well I'm trying to connect my database with vs (I'm not so familiar with it honestly )
and I'm trying to build a desktop app were i can assign my values and got them stored in my database (using postgresql btw)
can u help pls .
thank u
Posted
Updated 9-May-20 5:50am
Comments
MadMyche 9-May-20 13:06pm    
When you are debugging this; what is showing up from the Console.WriteLine or MessageBox.Show commands?

1 solution

You get this because you are trying to reference an item in a collection that doesn't exist. So when you do

something[0]


you reference the first item in "something"

something[1]


this references the second item and so on. If, however, there is only 1 item in "something" then you'll get the index out of range error. We don't have access to your collections so this isn't something we can fix for you, you'll need to use the debugger to examine your collections and work out why the items you think are there actually aren't.
 
Share this answer
 
Comments
MOUAD MOUMENE 9-May-20 12:01pm    
can u give more details pls
MOUAD MOUMENE 9-May-20 12:04pm    
i didn't get why my collection doesn't exist while i established the connection with my database (postgresql) , isn't my collection is the table of my database ??
F-ES Sitecore 9-May-20 12:35pm    
The problem will be with code like

dataGridView1.SelectedRows[2].Cells[1]

Either SelectedRows.Count is less than 3 or .Cells[1] is less than 2.
MOUAD MOUMENE 9-May-20 13:10pm    
when i did the SelectedRows.Count it showed me the value of 1
for cells it showed 6
F-ES Sitecore 9-May-20 13:17pm    
So you can't do .SelectedRows[2] as there aren't three selected rows.

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