Wrox Programmer Forums
|
ASP.NET 1.0 and 1.1 Basics ASP.NET discussion for users new to coding in ASP.NET 1.0 or 1.1. NOT for the older "classic" ASP 3 or the newer ASP.NET 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 1.0 and 1.1 Basics 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 12th, 2005, 10:48 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default problem updating using datagrid

--------------------------------------------------------------------------------

Hi, I am really having a problem with trying to get my datagrid to update my data base.

I have tried 2 different sets of codes, but when ever I enter the new data to be updated and click update, the datagrid will just return to the orginal datas. There is no updating done. I really am very new to this. Please advice what are the problems in my code that I must change. Thanks.

i use visual studio.net to write this codes.

First try:

Code:
public void SqlCmdConn(string sqlCmd)

            {

                  //First openning the connection to the db

                  SqlConnection conn = new SqlConnection("Initial Catalog=Boomerang; Server=(local); UID=sa; PWD=; ");



                  SqlCommand cmd = new SqlCommand(sqlCmd,conn);



                  conn.Open();

                  cmd.ExecuteNonQuery();

                  conn.Close();

            }





            public void updateRecords(object Sender, DataGridCommandEventArgs e)

            {

                  int custid1 = Convert.ToInt32(e.Item.Cells[2].Text);



                  TextBox titleTB = (TextBox)e.Item.Cells[3].Controls[0];

                  String title1 = Convert.ToString(titleTB.Text);



                  String strSql = "UPDATE customer_reg SET title ='" +title1+ "'WHERE custid = "+custid1+"";



                  SqlCmdConn sCC = new SqlCmdConn(strSql);





                  DataGrid1.EditItemIndex = -1;

                  DataGrid1.DataBind();

            }

Second try

Code:
public void updateRecords(object Sender, DataGridCommandEventArgs e)

            {



                  //Retrive the primary key(column) for the row to be edited.

                  int custid = Convert.ToInt16(e.Item.Cells[2].Text);

 

                  //Enable editing for the 1 column

                  TextBox titleTB = (TextBox)e.Item.Cells[3].Controls[0];

                  string title = Convert.ToString(titleTB.Text);

 

                  DataGrid1.EditItemIndex = -1;

                  UpdateTitle(custid, title);

 

                  DataSet ds = new DataSet();

                  DataGrid1.DataSource = ds.Tables["customer_reg"];

                  DataGrid1.DataBind();



 

 

 

            }

 

            private void UpdateTitle(int custid, string title)

            {

                  // Create and load a DataSet with records from BoomerandExt.customer_reg table

                  conn.Open();

 

                  String strConnection = "Initial Catalog=Boomerang; Server=(local); UID=sa; PWD=;";

                  String strSQLSelect = "SELECT * FROM customer_reg";

 

                  //string conn1 = "Initial Catalog=Boomerang; Server=(local); UID=sa; PWD=;";



                  //string cmd1 = "SELECT * FROM customer_reg";

 

 

                  SqlDataAdapter adapter = new SqlDataAdapter(strSQLSelect, strConnection);

                  DataSet ds = new DataSet();

                  adapt.Fill(ds, "customer_reg");

                  conn.Close();

 

                  // Modify the in-memory records in the DataSet

                  DataTable tbl = ds.Tables["customer_reg"];

                  tbl.PrimaryKey = new DataColumn[] {tbl.Columns["custid"]};

                  DataRow row = tbl.Rows.Find(custid);

                  row["title"] = title;

 

                  // Reconnect the DataSet and update the database

                  SqlCommandBuilder cb = new SqlCommandBuilder(adapt);

                  conn.Open();

                  adapt.Update(ds, "customer_reg");

                  conn.Close();

            }

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

Your second approach is somewhat correct, but there are better ways to go about it in ASP.NET.

A better approach for updates is to build UPDATE queries and just execute those directly instead of reloading the whole table and then updating the applicable rows.

You can do what you are doing with the updated dataset, but you need to create an UpdateCommand on the adapter so it knows how to update the records you changed in the table. I haven't worked with adapter update commands (because i'm working in ASP.NET) so you'll have to do some research on that.

-Peter
 
Old May 12th, 2005, 09:38 PM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, thanks, but how do i go about "to build UPDATE queries and just execute those directly instead of reloading the whole table and then updating the applicable rows." i don't know how this is done. Can you give me some pointers or an example. thanks.

 
Old May 13th, 2005, 12:01 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

You can find some sample code at
http://prashantprof.blogspot.com/200...in-aspnet.html
This might be helpful

Prashant

 
Old May 13th, 2005, 05:11 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Thanks the site really helped a lot. But now i am facing another problem. I can update and delete already. But my delete command when i click on the button, i must click twice before the row is being deleted away. What might be causing this problem. Please advice. Thanks

My delete code is :

private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
        {
            string key = DataGrid1.DataKeys[e.Item.ItemIndex].ToString();

            SqlCommand DeleteCommand = new SqlCommand("DELETE from customer_reg where custid='" + key + "'", sqlConnection1);

            //execute the command
            sqlConnection1.Open();
            DeleteCommand.ExecuteNonQuery();
            sqlConnection1.Close();

            //rebind the grid
            DataGrid1.EditItemIndex = -1;
            DataGrid1.DataBind();

        }



 
Old May 13th, 2005, 05:46 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

Hi,

What's happening in the first click?
Could you post the template column html?

Prashant

 
Old May 13th, 2005, 06:07 AM
Registered User
 
Join Date: May 2005
Posts: 4
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi, i don't know what u mean by the template column. This is my entire html codes. Please help thanks.

<%@ Page language="c#" Codebehind="FullEditableDataGrid.aspx.cs" AutoEventWireup="false" Inherits="AspdotnetWalkThroughs.WebForm1" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN" >
<HTML>
    <HEAD>
        <title>WebForm1</title>
        <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1">
        <meta name="CODE_LANGUAGE" Content="C#">
        <meta name="vs_defaultClientScript" content="JavaScript">
        <meta name="vs_targetSchema" content="http://schemas.microsoft.com/intellisense/ie5">
    </HEAD>
    <body MS_POSITIONING="GridLayout">
        <form id="Form1" method="post" runat="server">
            <asp:DataGrid id=DataGrid1 style="Z-INDEX: 101; LEFT: 24px; POSITION: absolute; TOP: 88px" runat="server" DataSource="<%# dsCustomer_reg1 %>" DataMember="customer_reg" DataKeyField="custid">
                <Columns>
                    <asp:EditCommandColumn ButtonType="LinkButton" UpdateText="Update" CancelText="Cancel" EditText="Edit"></asp:EditCommandColumn>
                    <asp:ButtonColumn Text="Delete" CommandName="Delete"></asp:ButtonColumn>
                </Columns>
            </asp:DataGrid>
        </form>
    </body>
</HTML>



 
Old May 13th, 2005, 07:32 AM
Friend of Wrox
 
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
Default

hi,

I counld not find any problem, It should work.
Check if you are not rebinding the grid unnessarily in every postback.
Put break points in all events and review the sequnce.

Prashant.






Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem updating data from a datagrid jazzydonald Visual Basic 2005 Basics 0 October 16th, 2007 06:16 AM
Problem updating a DataGrid RichardP ASP.NET 1.0 and 1.1 Professional 3 February 28th, 2006 11:42 AM
Problem getting updating Database from Datagrid RichardP ASP.NET 1.0 and 1.1 Basics 0 February 17th, 2006 07:55 AM
problem in updating datagrid noor ASP.NET 1.0 and 1.1 Basics 1 April 26th, 2005 03:10 AM





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