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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:12:01+00:00 2026-06-09T12:12:01+00:00

I am trying to fetch values on a GridView with the following code: protected

  • 0

I am trying to fetch values on a GridView with the following code:

protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
    GridViewRow row = GridView1.SelectedRow;
    string Username = row.Cells[3].Text;
    string Password = row.Cells[4].Text;
    string Email = row.Cells[5].Text;
    string ID_Inscricao = row.Cells[1].Text;
    SqlConnection sqlConn = new SqlConnection(
        ConfigurationManager.ConnectionStrings["FormacaoConnectionString"].ToString());
    SqlCommand sqlComm = new SqlCommand();

    sqlComm = sqlConn.CreateCommand();
    sqlComm.Parameters.Add("@Username", SqlDbType.Text);
    sqlComm.Parameters.Add("@Password", SqlDbType.Text);
    sqlComm.Parameters.Add("@Email", SqlDbType.Text);
    sqlComm.Parameters.Add("@ID_Inscricao", SqlDbType.Text);

    sqlComm.Parameters["@Username"].Value = Username;
    sqlComm.Parameters["@Password"].Value = Password;
    sqlComm.Parameters["@Email"].Value = Email;
    sqlComm.Parameters["@ID_Inscricao"].Value = ID_Inscricao;
    string sql = "INSERT INTO Utilizadores " +
        "(Username, Password, Email,ID_Inscricao) " +
        "VALUES (@Username, @Password, @Email, @ID_Inscricao)";
    sqlComm.CommandText = sql;
    sqlConn.Open();
    sqlComm.ExecuteNonQuery();
    sqlConn.Close();
}

So, the problem is I can’t get the values from the GridView, but instead get a “NullReferenceException was unhandled by user code”. Can anybody tell me what I’m doing wrong?

Best regards

  • 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-09T12:12:03+00:00Added an answer on June 9, 2026 at 12:12 pm

    Each line which looks like this:

    sqlComm.Parameters["@Username"].Value = Username;
    

    Requires that parameter to actually exist on the command object, but you’ve commented those lines out. Put back in one of these:

    sqlComm.Parameters.Add("@Username", SqlDbType.Text);
    

    For every parameter you’re setting the value for.


    Edited after comment

    Based on the fact that this line causes a NullReferenceException:

    string Username = row.Cells[3].Text;
    

    I can say for cetain that either

    a) row is null – there is no SelectedRow in your grid
    b) row.Cells is null, the selected row has no cells
    c) row.Cells[3] is null, the selected row has cells, but there is no cell 4(cells is zero based)

    There is no way of helping you further than that, You should set a breakpoint on the line that is failing and inspect each of the 3 cases above to see which one is null

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

Sidebar

Related Questions

I am trying to fetch wcf service from jquery. I have written below code
I am trying to fetch a PHP script using AJAX and return the values
I'm trying to fetch information from a JSON web service with this code, using
I'm trying to fetch to values from a plpgsql function with 2 OUT paramenters
I am trying to fetch the values from the database but i am getting
I am trying to retrieve values from Json Object but getting Exception.I am trying
I am trying to fetch the values of certain fields of a class based
I am trying to insert values to a csv file through the following script....the
I'm trying to fetch associations of a model dynamically. For ex; model1 has_many model2s
I'm trying to fetch data from a page that resides on an intranet. The

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.