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

  • Home
  • SEARCH
  • 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 6945191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T13:24:15+00:00 2026-05-27T13:24:15+00:00

I am using a asp.net grid view to load data from my sql data

  • 0

I am using a asp.net grid view to load data from my sql data table.I am able to successfully load the data.

Sql Table Desgin:
3 cloumns:Location,Firstname,LastName

Location is the primary key.

Design :

Aspxpage has a gridview which has two buttons in the bottom:

  1. Edit
  2. Save

When user hits Edit button all the cells in the gridview are made editable so that user can edit and save the values.

My problem arises with the save button where i am unable to save the edited data back to the SQL.

Here is the code for the save button click:

protected void btnSave_Click(object sender, EventArgs e)
{
    int RowIndex=0;

    GridViewRow row = (GridViewRow)gvres.Rows[RowIndex];

    TextBox txtLanguage1 = row.FindControl("txtFName") as TextBox;
    TextBox txtLanguage2 = row.FindControl("txtLName") as TextBox;

    SqlConnection myConnection = new SqlConnection(connectionString);
    SqlCommand cmd = new SqlCommand("UPDATE UsersTable SET FirstName = @FirstName, LastName = @LastName WHERE Location = @Location", myConnection);


    cmd.Parameters.AddWithValue("@FirstName", txtFirstName.Text.Trim());
    cmd.Parameters.AddWithValue("@LastName", txtLastName.Text.Trim());

    myConnection.Open();
    cmd.ExecuteNonQuery();
    gvusers.EditIndex = -1;
    DataBind();
}

Exception:“Must declare the scalar variable “@Location”.”

  • 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-05-27T13:24:16+00:00Added an answer on May 27, 2026 at 1:24 pm

    You need to add a parameter to the SqlCommand object named “@Location”. You mention that Location is one of the columns in your grid—you can either read the value from the column (similar to how you are getting the first and last name values), or you can specify “Location” as the data key, and get it from the DataKeys property of the grid.

    I’d look at the ASP.NET Real World Controls project on Codeplex. It allows for bulk editing and it smart enough to only update the rows that have changed.

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

Sidebar

Related Questions

I wrote a code in ASP.NET that read data from SQL Table and show
Using the ASP.NET grid view. It displays 3 columns with 1 row for each,
I am new in asp.net and working on the data grid view in asp.net.i
I am using the grid view in asp.net and i want to show the
If you are using ASP.NET MVC how are you doing grid display? Rolled your
I'm writing an asp.net mvc reservations app. in c# where in a grid view
I am trying to pass data between two pages using Session State in ASP.NET.
I am using ASP.NET MVC 3 with the razor view engine. I have the
I am currently developing an ASP.net c# application. I have a grid view which
As need to display Records in grid view in (ASP.NET) web site. and for

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.