Wrox Programmer Forums
|
ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old May 20th, 2005, 08:00 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default updating with datagrid

Dear All,
i am trying to edit a datagrid with update and cancel commands but on clicking "edit" its not replacing it with update and cancel buttons.
and the code for the update button is as follows:

private void dgemp_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            SqlConnection myconn=new SqlConnection(strconn);
            SqlCommand cmd;
            TextBox txtempname=(TextBox)( e.Item.Cells[1].Controls[0]);
            TextBox txtempsalary= (TextBox)(e.Item.Cells[2].Controls[0]);
            TextBox txtdeptname= (TextBox )(e.Item.Cells[3].Controls[0]);

            string strupdate;
            strupdate ="update employee set empname=@empname, empsalary=@empsalary, deptname=@deptname where empno=@empno";
            cmd=new SqlCommand(strupdate, myconn);
            cmd.Parameters.Add(new SqlParameter("@empname", txtempname.Text));
            cmd.Parameters.Add(new SqlParameter("@empsalary", txtempsalary.Text));
            cmd.Parameters.Add(new SqlParameter("@deptname", txtdeptname.Text));
            cmd.Parameters.Add(new SqlParameter("@empno", e.Item.Cells[0].Text));
            myconn.Open();
            cmd.ExecuteNonQuery ();
            dgemp.EditItemIndex =-1;
            myconn.Close();
            binddata();

}

plz help me out.

thanks and regards,
Muskaan.

 
Old May 20th, 2005, 09:10 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You need to get the grid into edit mode with a handler for the grid EditCommand event. The handler should contain something like this:

dgemp.EditItemIndex = e.Item.ItemIndex;
binddata();

This rebuilds the grid with the right edit row in edit mode. Then you get the update button that will fire the event handler you posted above.

-Peter
 
Old May 20th, 2005, 09:29 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx Peter,
i have done that with d following code.
but am not getting the buttons..

private void dgemp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dgemp.EditItemIndex =e.Item.ItemIndex;
            binddata();
        }

        private void dgemp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            dgemp.EditItemIndex =1;
            binddata();
        }

        private void binddata()
        {
            SqlConnection myconn= new SqlConnection(strconn);
            string mysql="select * from employee";
            SqlDataAdapter Ademp=new SqlDataAdapter(mysql, myconn);
            DataSet Ds=new DataSet();

            Ademp.Fill(Ds, "employee");
            dgemp.DataSource =Ds.Tables["employee"].DefaultView ;
            dgemp.DataBind();

        }

 
Old May 20th, 2005, 12:51 PM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You aren't getting the edit buttons?

Post your datagrid markup. You have an EditCommandColumn in the column list?

-Peter
 
Old May 23rd, 2005, 12:54 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanx Peter,
am sending u the whole code which is as follows :

public class UpdateDatagrd : System.Web.UI.Page
{
    protected System.Web.UI.WebControls.Label Label1;
    protected System.Web.UI.WebControls.DataGrid dgemp;

    string strconn="Data Source=sapnew; Initial Catalog=master; Integrated Security=SSPI";

private void Page_Load(object sender, System.EventArgs e)
{
    binddata();
}

private void dgemp_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    dgemp.EditItemIndex =e.Item.ItemIndex;
    binddata();
}

private void dgemp_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    dgemp.EditItemIndex =-1;
    binddata();
}

private void binddata()
{
    SqlConnection myconn= new SqlConnection(strconn);
    string mysql="select * from employee";
    SqlDataAdapter Ademp=new SqlDataAdapter(mysql, myconn);
    DataSet Ds=new DataSet();

    Ademp.Fill(Ds, "employee");
    dgemp.DataSource =Ds.Tables["employee"].DefaultView ;
    dgemp.DataBind();

}

private void dgemp_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    SqlConnection myconn=new SqlConnection(strconn);
    SqlCommand cmd;
    TextBox txtempname=(TextBox)( e.Item.Cells[1].Controls[0]);
    TextBox txtempsalary= (TextBox)(e.Item.Cells[2].Controls[0]);
    TextBox txtdeptname= (TextBox )(e.Item.Cells[3].Controls[0]);

    string strupdate;
        strupdate ="update employee set empname=@empname, empsalary=@empsalary, deptname=@deptname where empno=@empno";
    cmd=new SqlCommand(strupdate, myconn);
    cmd.Parameters.Add(new SqlParameter("@empname", txtempname.Text));
    cmd.Parameters.Add(new SqlParameter("@empsalary", txtempsalary.Text));
    cmd.Parameters.Add(new SqlParameter("@deptname", txtdeptname.Text));
    cmd.Parameters.Add(new SqlParameter("@empno", e.Item.Cells[0].Text));
    myconn.Open();
    cmd.ExecuteNonQuery ();
    dgemp.EditItemIndex =-1;
    myconn.Close();
    binddata();
}


and d datagrid markup is as follows :
<asp:DataGrid id="dgemp" style="Z-INDEX: 102; LEFT: 144px; POSITION: absolute; TOP: 88px" runat="server"
                ForeColor="#8080FF" BackColor="#C0FFFF" AutoGenerateColumns="False" Height="224px" Width="488px">
                <SelectedItemStyle ForeColor="#FFFFC0" BackColor="#C0C0FF"></SelectedItemStyle>
                <AlternatingItemStyle BackColor="#FFC0FF"></AlternatingItemStyle>
                <Columns>
                    <asp:BoundColumn DataField="empno" HeaderText="Employee ID"></asp:BoundColumn>
                    <asp:BoundColumn DataField="empname" HeaderText="Employee Name"></asp:BoundColumn>
                    <asp:BoundColumn DataField="empsalary" HeaderText="Employee Salary"></asp:BoundColumn>
                    <asp:BoundColumn DataField="deptname" HeaderText="Department Name"></asp:BoundColumn>
                    <asp:ButtonColumn Text="Select" ButtonType="PushButton" CommandName="Select"></asp:ButtonColumn>
                    <asp:EditCommandColumn ButtonType="PushButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                </Columns>
            </asp:DataGrid>

Thanks & Regards,
Muskaan.

 
Old May 23rd, 2005, 03:47 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hey Peter,
now i am getting the buttons "Edit", "Update" and "Cancel" after specifying Postback in the page_load().

but it is not updating the fields.

Regards,
Muskaan.

 
Old May 23rd, 2005, 04:28 AM
Authorized User
 
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
Default

I am now able to update but the prb is that it is updating all d records in d database with that same value.

private void dgemp_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
    SqlConnection myconn=new SqlConnection(strconn);
    SqlCommand cmd;
    TextBox txtempname=(TextBox)( e.Item.Cells[1].Controls[0]);
    TextBox txtempsalary= (TextBox)(e.Item.Cells[2].Controls[0]);
    TextBox txtdeptname= (TextBox )(e.Item.Cells[3].Controls[0]);

    string strupdate;
    strupdate ="update employee set empname=@empname, empsalary=@empsalary, deptname=@deptname where empno=@empno";
    cmd=new SqlCommand(strupdate, myconn);
    cmd.Parameters.Add(new SqlParameter("@empname", txtempname.Text));
    cmd.Parameters.Add(new SqlParameter("@empsalary", txtempsalary.Text));
    cmd.Parameters.Add(new SqlParameter("@deptname", txtdeptname.Text));
    cmd.Parameters.Add(new SqlParameter("@empno", e.Item.Cells[0].Text));
    myconn.Open();
    cmd.ExecuteNonQuery();
    dgemp.EditItemIndex = -1;
    myconn.Close();

    binddata();

}

private void binddata()
{
    SqlConnection myconn= new SqlConnection(strconn);
    string mysql="select empno, empname, empsalary, deptname from employee";
        SqlDataAdapter Ademp=new SqlDataAdapter(mysql, myconn);
    DataSet Ds=new DataSet();

    Ademp.Fill(Ds, "employee");
    dgemp.DataSource =Ds.Tables["employee"].DefaultView ;
    dgemp.DataBind();
}

Plz help me out.

Thanks & Regards,
Muskaan :)

 
Old May 23rd, 2005, 07:44 AM
planoie's Avatar
Friend of Wrox
 
Join Date: Aug 2003
Posts: 5,407
Thanks: 0
Thanked 16 Times in 16 Posts
Default

You're going to have to modify your SQL update statement to update only the records you want. If the query values are correct you'll have to debug it to see what the values are that you are assigning to the stored procedure call.

-Peter





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating a datagrid jgrant ASP.NET 2.0 Basics 0 August 25th, 2007 07:34 PM
Problem updating a DataGrid RichardP ASP.NET 1.0 and 1.1 Professional 3 February 28th, 2006 11:42 AM
problem updating using datagrid bananaking ASP.NET 1.0 and 1.1 Basics 7 May 13th, 2005 07:32 AM
problem in updating datagrid noor ASP.NET 1.0 and 1.1 Basics 1 April 26th, 2005 03:10 AM
Updating DataGrid without PostBack/Refresh Baby_programmer ASP.NET 1.0 and 1.1 Professional 13 April 11th, 2005 04:47 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.