Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7717735
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T02:58:42+00:00 2026-06-01T02:58:42+00:00

I have a datagridview in c# that i am filling from a mysql database.

  • 0

I have a datagridview in c# that i am filling from a mysql database. The user reviews the info and then saves it to a different mysql database. I know the connections to both work and like my title says I’m getting a null exception on the first string in the for loop.

there are only 6 columns in the row and the only thing i can think of is i am looking at rows and not columns but not sure what the code should be for that edit I changed it as most had said to < instead of <= and I know why i get the error but don’t know how to get the information i need. The error is coming from the fact that the cell shows as null even though there is a value in the cell.

string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
MySqlConnection connection2;
connection2 = new MySqlConnection(ConnectionString2);

int i;
for (i = 0; i < dataGridView1.Rows.Count; i++)
{
    string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
    string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
    string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
    string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
    string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
    string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();

    MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
    MySqlParameter propid = new MySqlParameter("@propid", b);
    MySqlParameter day1 = new MySqlParameter("@day", day);
    MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
    MySqlParameter vol1 = new MySqlParameter("@vol", vol);
    MySqlParameter prod1 = new MySqlParameter("@prod", prod);
    MySqlParameter qty1 = new MySqlParameter("@qty", qty);
    MySqlParameter item1 = new MySqlParameter("@item", item);

    cmdwk.Parameters.Add(day1);
    cmdwk.Parameters.Add(propid);
    cmdwk.Parameters.Add(Desc1);
    cmdwk.Parameters.Add(prod1);
    cmdwk.Parameters.Add(vol1);
    cmdwk.Parameters.Add(qty1);
    cmdwk.Parameters.Add(item1); 
}

The datagrid is called and filled in the load this is done on a button
Edit Seeing I am one point away from my img post ability I will try to explain this more
When the button is clicked and this code runs the error comes up at

string day = dataGridView1.Rows[i].Cells[0].Value.ToString();

the error is

Object reference not set to an instance of an object.

System.NullReferenceException was unhandled
  HResult=-2147467261
   Message=Object reference not set to an instance of an object.
    Source=SpectLabRemake2
    StackTrace:
   at SpectLabRemake2.Form6.btn_Save_Click(Object sender, EventArgs e) 
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T02:58:44+00:00Added an answer on June 1, 2026 at 2:58 am

    Well for everyone that has this problem I will post my code that fixed the issue and Thank YOU soo much to everyone that was SOOOO helpful here with this issue.

      for (i = 0; i < dataGridView1.Rows.Count-1; i++)
            {
    
                string ConnectionString2 = ConfigurationSettings.AppSettings["ConnectionString2"];
                MySqlConnection connection2;
    
    
    
    
    
    
    
    
                connection2 = new MySqlConnection(ConnectionString2);
                connection2.Open();
    
    
                    string day = dataGridView1.Rows[i].Cells[0].Value.ToString();
                    string Desc = dataGridView1.Rows[i].Cells[1].Value.ToString();
                    string item = dataGridView1.Rows[i].Cells[2].Value.ToString();
                    string prod = dataGridView1.Rows[i].Cells[3].Value.ToString();
                    string vol = dataGridView1.Rows[i].Cells[4].Value.ToString();
                    string qty = dataGridView1.Rows[i].Cells[5].Value.ToString();
    
    
                    MySqlCommand cmdwk = new MySqlCommand("INSERT INTO spt_proposal_line_lab (proposal_Id,day_Name,proposal_Desc,proposal_Vol,product_Id,proposal_Qty,item_Id) VALUES (@propid,@day,@desc,@vol,@prod,@qty,@item)", connection2);
                    MySqlParameter propid = new MySqlParameter("@propid", b);
                    MySqlParameter day1 = new MySqlParameter("@day", day);
                    MySqlParameter Desc1 = new MySqlParameter("@desc", Desc);
                    MySqlParameter vol1 = new MySqlParameter("@vol", vol);
                    MySqlParameter prod1 = new MySqlParameter("@prod", prod);
                    MySqlParameter qty1 = new MySqlParameter("@qty", qty);
                    MySqlParameter item1 = new MySqlParameter("@item", item);
    
                    cmdwk.Parameters.Add(day1);
                    cmdwk.Parameters.Add(propid);
                    cmdwk.Parameters.Add(Desc1);
                    cmdwk.Parameters.Add(prod1);
                    cmdwk.Parameters.Add(vol1);
                    cmdwk.Parameters.Add(qty1);
                    cmdwk.Parameters.Add(item1);
                    cmdwk.ExecuteNonQuery();
    
    
    
                }
            }
    
        }
    }
    

    Brent

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a DataGridView that displays data from a MS Access database. I'm using
I have a DataGridView that has MultiSelect = true. After the user selects different
I have this problem: I have a datagridview that reads the data from a
I have an automatically binded DataGridView that obtains data and update data directly from
I have a datagridview that i bind from an sql table, in that dv
I have a datagridview that displays data from an XML file. The DGV is
I have a DataGridView that I'm populating from a list. The function that edits
I have a dataGridView that has a column with checkboxes. Whenever the user clicks
I have a DataGridView that is not connected to a database. I manually insert
I have a DataGridView in a WinForm. I fill the DataGridView from a database

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.