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 8379707
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T16:15:47+00:00 2026-06-09T16:15:47+00:00

I am having a DataGridView and an Add New Entry button. Each time I

  • 0

I am having a DataGridView and an Add New Entry button. Each time I add a new entry to the database, I want the program to select the row of the new entry in the DataGridView. After clicking on the Add New Entry button, the below function will be called, with parameters of studentName and date passed to the function. The name of the DataGridView is dvgPontengHistory.

But there is an exception thrown:

Object reference not set to an instance of an object.

on this line:

if(r.Cells["student_name"].Value.ToString().Contains(studentName))

Below is the code:

   private void selectRow(string studentName, string date)
    {
        int i = 0;
        foreach (DataGridViewRow r in dgvPontengHistory.Rows)
        {
            if(r.Cells["student_name"].Value.ToString().Contains(studentName)) // error in this line
            {
                if (r.Cells["date"].Value.ToString().Contains(date))
                {
                    dgvPontengHistory.Rows[i].Selected = true;
                    return;
                }
            }
            i++;
        }
    }

Any tips on resolving this problem? Thanks.

  • 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-09T16:15:48+00:00Added an answer on June 9, 2026 at 4:15 pm

    Probably the case that student_name was null in on of the rows in the result set. That would cause ToString() to fail.

    My guess is your update statement is putting null into your table.

    Here is how you test:

    (set breakpoints on the throw lines).

      private void selectRow(string studentName, string date)
        {
            int i = 0;
            foreach (DataGridViewRow r in dgvPontengHistory.Rows)
            {
                if (r.Cells["student_name"] == null) { throw("can't find cell"); }
                if(r.Cells["student_name"].Value == null) { throw("cell has no value"); }
                if(r.Cells["student_name"].Value.ToString().Contains(studentName)) // error in this line
                {
                    if (r.Cells["date"].Value.ToString().Contains(date))
                    {
                        dgvPontengHistory.Rows[i].Selected = true;
                        return;
                    }
                }
                i++;
            }
        }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm having trouble saving a new row in the DataGridView back to the database.
i have one datagridview.in my datagridview is having four row and three columns.after enter
Having just added a new button in my web application, I get an error
How can i insert each time a diffrent row in datatable? The data table
I am having a datagridview created as follows on button click private void button1_Click(object
I'm having problem in my datagridview. I'm using vb 2008 and an access database.
I am having trouble binding some data from my MySQL database to a DataGridView
I am having problems selecting a row out of the DataGridView on a search.
I would like to extended DataGridView to add a second ContextMenu which to select
Having trouble displaying MonthName in a DataGridView from data in MySQL Query: SELECT ItemName,

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.