Solved

update datagrid

Posted on 2009-03-31
3
350 Views
Last Modified: 2012-08-13
I'm just trying a simple example to update a datagrid.

I'm not getting any errors, but the grid does not update.

See my code below.

Thanks for any help.
aspx page:
 
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="Default.aspx.cs" Inherits="SqlDataAdapterExample._Default" %>
 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
 
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
        <div>      
            <asp:Button runat="server" ID="btnFillAdapter" Text="Fill DataAdapter" onclick="btnFillAdapter_Click" />
            
            <br />
            <br />
            
            <asp:DataGrid 
                runat="server" 
                ID="dgAuthors" 
                oncancelcommand="dgAuthors_CancelCommand" 
                ondeletecommand="dgAuthors_DeleteCommand" 
                oneditcommand="dgAuthors_EditCommand" 
                onupdatecommand="dgAuthors_UpdateCommand" 
                AutoGenerateColumns="False" 
                Width="850px">
                <Columns>
                    <asp:EditCommandColumn 
                        ButtonType="PushButton" 
                        CancelText="Cancel" 
                        EditText="Edit" 
                        UpdateText="Update"></asp:EditCommandColumn>
                    <asp:ButtonColumn 
                        ButtonType="PushButton" 
                        CommandName="Delete" 
                        Text="Delete">
                    </asp:ButtonColumn>
                    <asp:BoundColumn DataField="au_fname"></asp:BoundColumn>
                    <asp:BoundColumn DataField="au_lname"></asp:BoundColumn>
                    <asp:BoundColumn DataField="address"></asp:BoundColumn>
                    <asp:BoundColumn DataField="city"></asp:BoundColumn>
                    <asp:BoundColumn DataField="state"></asp:BoundColumn>
                </Columns>
            </asp:DataGrid>
            <asp:TextBox runat="server" ID="txtLog" TextMode="MultiLine" Rows="20" Columns="85" ></asp:TextBox>
        </div>
    </form>
</body>
</html>
 
 
code-behind:
 
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Data;
 
namespace SqlDataAdapterExample
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection conPubs = new SqlConnection();
        SqlCommand cmdSelectAuthor = new SqlCommand();
        SqlCommand cmdInsertAuthor = new SqlCommand();
        SqlCommand cmdUpdateAuthor = new SqlCommand();
        SqlCommand cmdDeleteAuthor = new SqlCommand();
 
        SqlDataAdapter daAuthor = new SqlDataAdapter();
 
        DataSet dsAuthor = new DataSet();
 
        protected void Page_Load(object sender, EventArgs e)
        {
            conPubs.ConnectionString = "data source=DEPPS\\SQLExpress;initial catalog=pubs;user id=dwayne;password=Z1x2c3v4;persist security info=True";
 
            daAuthor.SelectCommand = cmdSelectAuthor;
            daAuthor.InsertCommand = cmdInsertAuthor;
            daAuthor.UpdateCommand = cmdUpdateAuthor;
            daAuthor.DeleteCommand = cmdDeleteAuthor;
 
            daAuthor.SelectCommand.CommandText = "SELECT * FROM Authors";
            daAuthor.SelectCommand.Connection = conPubs;
 
            daAuthor.UpdateCommand.CommandText = "UPDATE Authors SET au_id = @au_id, au_lname = @au_lname, au_fname = @au_fname, phone = @phone, address = @address, city = @city, state = @state, zip = @zip, contract = @contract";
            daAuthor.UpdateCommand.Connection = conPubs;
            daAuthor.UpdateCommand.Parameters.Add("@au_id", SqlDbType.Char, 11, "au_id");
            daAuthor.UpdateCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40, "au_lname");
            daAuthor.UpdateCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20, "au_fname");
            daAuthor.UpdateCommand.Parameters.Add("@phone", SqlDbType.Char, 12, "phone");
            daAuthor.UpdateCommand.Parameters.Add("@address", SqlDbType.VarChar, 40, "address");
            daAuthor.UpdateCommand.Parameters.Add("@city", SqlDbType.VarChar, 20, "city");
            daAuthor.UpdateCommand.Parameters.Add("@state", SqlDbType.VarChar, 2, "state");
            daAuthor.UpdateCommand.Parameters.Add("@zip", SqlDbType.Char, 5, "zip");
            daAuthor.UpdateCommand.Parameters.Add("@contract", SqlDbType.Bit, 1, "contract");
 
            daAuthor.DeleteCommand.CommandText = "DELETE FROM Authors WHERE au_id = @au_id";
            daAuthor.DeleteCommand.Connection = conPubs;
            daAuthor.DeleteCommand.Parameters.Add("@au_id", SqlDbType.Char, 11, "au_id");
 
            daAuthor.InsertCommand.CommandText = "INSERT INTO Authors (au_id, au_lname, au_fname, phone, address, city, state, zip, contract) VALUES (@au_id, @au_lname, @au_fname, @phone, @address, @city, @state, @zip, @contract)";
            daAuthor.InsertCommand.Connection = conPubs;
            daAuthor.InsertCommand.Parameters.Add("@au_id", SqlDbType.Char, 11, "au_id");
            daAuthor.InsertCommand.Parameters.Add("@au_lname", SqlDbType.VarChar, 40, "au_lname");
            daAuthor.InsertCommand.Parameters.Add("@au_fname", SqlDbType.VarChar, 20, "au_fname");
            daAuthor.InsertCommand.Parameters.Add("@phone", SqlDbType.Char, 12, "phone");
            daAuthor.InsertCommand.Parameters.Add("@address", SqlDbType.VarChar, 40, "address");
            daAuthor.InsertCommand.Parameters.Add("@city", SqlDbType.VarChar, 20, "city");
            daAuthor.InsertCommand.Parameters.Add("@state", SqlDbType.VarChar, 2, "state");
            daAuthor.InsertCommand.Parameters.Add("@zip", SqlDbType.Char, 5, "zip");
            daAuthor.InsertCommand.Parameters.Add("@contract", SqlDbType.Bit, 1, "contract");
        }
 
        protected void BindData()
        {
            conPubs.Open();
            daAuthor.Fill(dsAuthor, "Authors");
            conPubs.Close();
            dgAuthors.DataSource = dsAuthor;
            dgAuthors.DataBind();
        }
 
        protected void btnFillAdapter_Click(object sender, EventArgs e)
        {
            BindData();
        }
 
 
        protected void daAuthor_RowUpdating(object sender, SqlRowUpdatingEventArgs e)
        {
            txtLog.Text += "Updating row ... ";
            txtLog.Text += "<br />" + "Status: " + e.Status.ToString();
            txtLog.Text += "<br />" + "Type: " + e.StatementType.ToString();
            txtLog.Text += "<br />" + "Command: " + e.Command.CommandText.ToString();
            txtLog.Text += "<br />" + "Author ID: " + e.Row["au_id"].ToString();
        }
 
        protected void daAuthor_RowUpdated(object sender, SqlRowUpdatedEventArgs e)
        {
            txtLog.Text += "Row updated ... ";
            txtLog.Text += "<br />" + "Status: " + e.Status.ToString();
            txtLog.Text += "<br />" + "Type: " + e.StatementType.ToString();
            txtLog.Text += "<br />" + "Command: " + e.Command.CommandText.ToString();
            txtLog.Text += "<br />" + "Author ID: " + e.Row["au_id"].ToString();
            txtLog.Text += "<br />" + "Rows Affected: " + e.RecordsAffected.ToString();
        }
 
        protected void daAuthor_FillError(object sender, FillErrorEventArgs e)
        {
            txtLog.Text += "Error filling data adapter ... ";
            txtLog.Text += "<br />" + "Table: " + e.DataTable.ToString();
            txtLog.Text += "<br />" + "Error Message: " + e.Errors.Message.ToString();
            txtLog.Text += "<br />" + "Error StackTrace: " + e.Errors.StackTrace.ToString();
        }
 
        protected void dgAuthors_CancelCommand(object source, DataGridCommandEventArgs e)
        {
            dgAuthors.EditItemIndex = -1;
            BindData();
 
        }
 
        protected void dgAuthors_EditCommand(object source, DataGridCommandEventArgs e)
        {
            dgAuthors.EditItemIndex = e.Item.ItemIndex;
            BindData();
        }
 
        protected void dgAuthors_DeleteCommand(object source, DataGridCommandEventArgs e)
        {
 
        }
 
        protected void dgAuthors_UpdateCommand(object source, DataGridCommandEventArgs e)
        {
            daAuthor.UpdateCommand = cmdUpdateAuthor;
            dgAuthors.EditItemIndex = -1;
            BindData();
        }
    }
}

Open in new window

0
Comment
Question by:-Dman100-
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 9

Expert Comment

by:cdaly33
ID: 24035916
I don't see where you're doing the BindData in Page_Load to actually bind the datasource to the datagrid.
0
 

Author Comment

by:-Dman100-
ID: 24036190
I'm using the button click event handler to bind to the grid:

protected void btnFillAdapter_Click(object sender, EventArgs e)
        {
            BindData();
        }
0
 
LVL 9

Accepted Solution

by:
cdaly33 earned 500 total points
ID: 24038843
I think the next steps would be to fire up SQL Profiler.  Initiate the update and see if the statement is being run on the server.  If its not being run then you'd know that its not firing at all.  If you do see the statement being run in profiler then take that statement from profiler and try running it manually in Query Analyzer to see if the issue is with the sql thats being run.
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
Come and listen to Percona CEO Peter Zaitsev discuss what’s new in Percona open source software, including Percona Server for MySQL (https://www.percona.com/software/mysql-database/percona-server) and MongoDB (https://www.percona.com/software/mongo-…
Monitoring a network: why having a policy is the best policy? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the enormous benefits of having a policy-based approach when monitoring medium and large networks. Software utilized in this v…

705 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question