Click here to Skip to main content
15,885,188 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
How can I add

C#
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;
            GridView1.DataBind();

this is relation between tables:
<img src='http://images.uvl.ir/img/e5eeca522e12.png' alt='Image Hosted by Free Photo Hosting at http://www.iranxm.com/' />


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?

Thanks
please help!
Posted
Updated 14-Feb-13 8:16am
v2

AutoGenerateColumns="false"


like

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


[Moved from 2nd solution]
http://www.aspdotnet-suresh.com/2011/02/how-to-inserteditupdate-and-delete-data.html[^]

watch this sample code..
 
Share this answer
 
v3
Comments
jiji2663 14-Feb-13 8:40am    
thanks
it's work
but i get another error when i click on button "Update"
error:
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    
solver
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...
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();
       adp.Fill(ds);
       GridView1.DataSource = ds;
       GridView1.DataBind();


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:

C#
gridShow.EditIndex = e.NewEditIndex;
      show();



similarly,
under RowCancelling event:

C#
GridView1.EditIndex = -1;
        show();


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();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
       
        GridView1.EditIndex = -1;
        
        show();



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;

        con.Open();
        cmd.ExecuteNonQuery();
        cmd.Dispose();
        show();


just tweak the code and it will work for you.
 
Share this answer
 
Comments
jiji2663 14-Feb-13 6:45am    
thanks for your help
but i have a new problem when i run the project,se this picture:
http://www.uploadimage.in/di/39FN/2-14-2013 3-08-34 PM.png

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



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900