Click here to Skip to main content
15,917,793 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi Friends
I am using window application

I am trying to save data from data grid to sql sever but i am getting the error

Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index


The i have written on update button is

C#
try
            {
                string section_id = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string mid = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string section_name = dataGridView1[2, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string sdt = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string indexorder = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
               // string col5 = comboBox1 4,comboBox1.SelectedItem.ToString();

                string insert_sql = "insert into magazinsectionmaster(section_id,mid,section_name,sdt,indexorder)values('" + section_id + "','" + mid + "','" + section_name + "','" + sdt + "','" + indexorder + "')", sc;

                if (this.getcomm(insert_sql))
                {
                    MessageBox.Show("Insert Success");
                }
                else
                {
                    MessageBox.Show("Insert Failed");
                }
            }
            catch
            {
                throw;

            }


When i put the break point it runs to section and mid then directly it goes to catch and give me error


Please help me in this.

Thanks in advance
Posted
Comments
Prasad_Kulkarni 3-Apr-12 6:54am    
can you give your db structure?
Error must be at your insert query, am i right?
sonu_coder 3-Apr-12 7:00am    
string insert_sql = "insert into magazinsectionmaster(section_id,mid,section_name,sdt,indexorder)values('" + section_id + "','" + mid + "','" + section_name + "','" + sdt + "','" + indexorder + "')", sc;

Prasad_Kulkarni 3-Apr-12 7:02am    
i was expecting db structure sonu87.
sonu_coder 3-Apr-12 7:18am    
which db you want ? i m bit confuse. and my name is only sonu
Prasad_Kulkarni 3-Apr-12 8:34am    
You must be inserting this record in database somewhere for which you might have created a table,in that you are going to fire this insert query..that table structure i want to see..This error might because of; you're trying to insert either max range of data and database value is lower one..

I have solve by self but i can able to save one data at a time.

C#
public bool getcomm(string connstring1)
        {
            try
            {
                //SqlConnection sqlcon = this.getsqlcon();
                SqlCommand sqlcomm = new SqlCommand(connstring1, con);
                con.Open();
                sqlcomm.ExecuteNonQuery();
                sqlcomm.Dispose();
                con.Close();
                con.Dispose();
                return true;

            }
            catch (Exception ex)
            {
                return false;
            }



C#
private void btnupdate_Click(object sender, EventArgs e)
        {
            try
            {
              //  string section_id = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();

                //int mid = Convert.ToInt32(dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString());
                //string mid = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string section_name = dataGridView1[0, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                //string sdt = dataGridView1[3, dataGridView1.CurrentCell.RowIndex].Value.ToString();
                string indexorder = dataGridView1[1, dataGridView1.CurrentCell.RowIndex].Value.ToString();

                // string col5 = comboBox1 4,comboBox1.SelectedItem.ToString();

                string insert_sql = "insert into magazinesectionmaster( section_name, indexorder)values('" + section_name + "','" + indexorder + "')", con;

                if (this.getcomm(insert_sql))
                {
                    MessageBox.Show("Insert Success");
                }
                else
                {
                    MessageBox.Show("Insert Failed");
                }
            }
            catch
            {
                throw;
            }
 
Share this answer
 
you can use foreach loop to read grid view rows like this

C#
foreach (DataGridViewRow row in dataGridView1.Rows)
{
if(dataGridView1["your column name", row.Index].Value!=null)
{
  string section_name = dataGridView1["your column name", row.Index].Value.ToString();
}
}


instead use column index try to use column name as string.

i hope this will help you.
 
Share this answer
 
v2

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