hello to everyone i have a program that can update the data that you've edited in the gridview design the problem is whenever i have 3 records in my database and i edited one of them in the grid view all of them gets updated.. can anyone help me with my program.. i post my code below.. pls help..
PS: my database is on MS ACCESS..

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.OleDb;
public partial class Delete : System.Web.UI.Page
{
    string stcron = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=C:\\Database\\db1.mdb;Persist Security Info=False";
    System.Data.OleDb.OleDbConnection cn = new System.Data.OleDb.OleDbConnection();
    System.Data.OleDb.OleDbDataAdapter da = new System.Data.OleDb.OleDbDataAdapter();
    System.Data.OleDb.OleDbCommand dc = new System.Data.OleDb.OleDbCommand();


    protected void Page_Load(object sender, EventArgs e)
    {
        AccessDataSource1.DataFile = ConfigurationSettings.AppSettings["dbLocation"].ToString();
        cn = new System.Data.OleDb.OleDbConnection(stcron);
        DataTable dt = new DataTable();
    }


    protected void AccessDataSource1_Updating(object sender, SqlDataSourceCommandEventArgs e)
    {
        cn.Open();
        dc = cn.CreateCommand();


    }
    protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
    {

    }

    protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
   {
     TextBox txtbox1 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox1");
     TextBox txtbox2= (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2");
     TextBox txtbox3 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3");
     TextBox txtbox4 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox4");
     TextBox txtbox5 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox5");
     TextBox txtbox6 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox6");
     TextBox txtbox7 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox7");
     TextBox txtbox8 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox8");
     TextBox txtbox9 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox9");
     TextBox txtbox10 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox10");
     TextBox txtbox11 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox11");
     TextBox txtbox12 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox12");
     TextBox txtbox13 = (TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox13");

        if (txtbox1 != null)
        {

            cn.Open();
            dc = cn.CreateCommand();
            string strQry = @"UPDATE tblUser SET LastName= '" + txtbox1.Text + "', FirstName='"+ txtbox2.Text+"', MiddleName='"+txtbox3.Text+"', Address='" +txtbox4.Text+"', TelNo='"+txtbox5.Text+"', CelNo='"+txtbox6.Text+"', EmailAdd='"+txtbox7.Text+"',Nationality='"+txtbox8.Text+"',[Marital Status]='"+txtbox9.Text+"', Gender='"+txtbox10.Text+"', Age='"+txtbox11.Text+"', BirthDate='"+txtbox12.Text+"',BirthPlace='"+txtbox13.Text+"'";

               // if (txtbox1.Text != null && txtbox1.Text != "")
               // MsgBox1.alert("Please input something in the text box.");

            dc.CommandText = strQry;
            dc.ExecuteNonQuery();
            cn.Close();
            Page_Load(null, null);
        }
       }

pls.. can anyone help me edit my codes..

Edited 3 Years Ago by Dani: Formatting fixed

Your update comand, in a simple form is

Update tblUser set Lastname = ABCDEFG

How do you expect SQL to know what Lastname you want to update?

You need to qualify it

Update tblUser set Lastname = ABCDEFG WHERE USERID = 1234

that will only update the single record.

Also you are better using a querystring like:

Update tblUser set Lastname = @LastName where UserID = @UserID
dc.parameters.addwithvalue("@LastName", textbox1.text)
dc.parameters.addwithvalue("@UserID",1234)
conn1.open
dc.execute...
conn1.close
This article has been dead for over six months. Start a new discussion instead.