Click here to Skip to main content
13,250,182 members (69,418 online)
Rate this:
Please Sign up or sign in to vote.
See more:
How can I add

DataTable dt = new DataTable();
            Dal.Fill_Dt("SELECT Tbl_NwsLtr.ID_NwsLtr , Tbl_NwsLtr.FlNm , Tbl_NwsLtr._ml,Tbl_NwsLtr._MbN, Tbl_Job.JbName, Tbl_NwsLtr._dt FROM Tbl_NwsLtr INNER JOIN Tbl_Job ON Tbl_NwsLtr._Jb_ID = Tbl_Job._Jb_ID", dt);
            GridView1.DataSource = dt;

this is relation between tables:
<img src='' alt='Image Hosted by Free Photo Hosting at' />

How can I edit,update,and add data in code behinde
when the user click on the "Edit" button? I want to show field JbName into dropdownlist.
how can I do this?

please help!
Posted 11-Feb-13 22:02pm
Updated 14-Feb-13 9:16am
Rate this: bad
Please Sign up or sign in to vote.

Solution 3



<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false">

[Moved from 2nd solution][^]

watch this sample code..
jiji2663 14-Feb-13 8:40am
it's work
but i get another error when i click on button "Update"
nvalid postback or callback argument. Event validation is enabled using <pages enableEventValidation="true"/> in configuration or <%@ Page EnableEventValidation="true" %> in a page. For security purposes, this feature verifies that arguments to postback or callback events originate from the server control that originally rendered them. If the data is valid and expected, use the ClientScriptManager.RegisterForEventValidation method in order to register the postback or callback data for validation.
jiji2663 14-Feb-13 9:12am
i use
IsPostBack in page load
jiji2663 14-Feb-13 9:14am
i have new problem's
when i edit field and ckick on button "Update"
get this error:
Object reference not set to an instance of an object
a2ulthakur 14-Feb-13 10:06am
Make sure you are using correct datatypes for all the fields you are trying to update in RowUpdating event in gridview and use breakpoints
Avik Ghosh22 14-Feb-13 10:49am
<pages enableEventValidation="true"/> error come when u don't use ispostback false in your page load part.... Object reference not set to an instance of an object means there are some error in your code ..... check your code with break point...
jiji2663 14-Feb-13 11:08am
i try this
but don't work
jiji2663 14-Feb-13 11:09am
this error just for:
string id = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("ID_Order")).Text;
i change it to:
string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[1].FindControl("ID_Order")).Text;
string id = ((TextBox)GridView1.Rows[e.RowIndex].Cells[0].FindControl("ID_Order")).Text;
but get same error
Avik Ghosh22 14-Feb-13 11:13am
bro check this .... main problem in your can not recognize the textbox ... ID_Order is this textbox id???? i am sorry for my poor English...
Avik Ghosh22 14-Feb-13 11:15am
TextBox txtUsrname = (TextBox)gvDetails.FooterRow.FindControl("txtboxid");
Avik Ghosh22 14-Feb-13 13:19pm
Marcus Kramer thank u sir....
Rate this: bad
Please Sign up or sign in to vote.

Solution 1

ok first of all make a function from which u will populate your gridview like this :
public void show()
       string str = "select * from table2";
       SqlDataAdapter adp = new SqlDataAdapter(str, con);
       DataSet ds = new DataSet();
       GridView1.DataSource = ds;

then go to your gridview and click on the smart tag from there select uption edit columns and add a new command feild "edit, update and cancel"
now go to gridview events

double click the RowEditing event:

gridShow.EditIndex = e.NewEditIndex;

under RowCancelling event:

GridView1.EditIndex = -1;

under RowUpdating event:
before doing that you should know that you should convert your gridview columns into only template fields go to smart tag of gridview and click edit template and then for the columns you dont want to edit insert a label in their edititemtemplate and the fields you want to change add a textbox

int id =Convert.ToInt32( ((Label)GridView1.Rows[e.RowIndex].FindControl("Label4")).Text);
        string userid = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox2")).Text;
        string pwd = ((TextBox)GridView1.Rows[e.RowIndex].FindControl("TextBox3")).Text;
        string str = "update table2 set userid='" + userid + "',pwd='" + pwd + "' where id=" + id;
        SqlCommand cmd = new SqlCommand(str, con);
        cmd.Connection = con;
       // con.Open();
        GridView1.EditIndex = -1;

delete is done like this:
int id = Convert.ToInt32(((Label)GridView1.Rows[e.RowIndex].FindControl("Label12")).Text);
        string del = "Delete from monthly where id="+id;
        SqlCommand cmd = new SqlCommand(del, con);
        cmd.Connection = con;

just tweak the code and it will work for you.
jiji2663 14-Feb-13 6:45am
thanks for your help
but i have a new problem when i run the project,se this picture: 3-08-34 PM.png

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month

Advertise | Privacy |
Web03 | 2.8.171114.1 | Last Updated 14 Feb 2013
Copyright © CodeProject, 1999-2017
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100