Wrox Programmer Forums
|
Classic ASP Databases Discuss using ASP 3 to work with data in databases, including ASP Database Setup issues from the old P2P forum on this specific subtopic. See also the book forum Beginning ASP.NET Databases for questions specific to that book. NOT for ASP.NET 1.0, 1.1, or 2.0.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the Classic ASP Databases 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 August 20th, 2004, 03:41 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default Updating to SQL Database

I am new to .NET and I have a big problem that I couldn't find a solution for. I am not able to update the edited data from the datagrid. Please Help! What am I doing wrong? I am getting this error when trying to view the site on IE: The IListSource does not contain any data sources. Here is my code:

sing System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;

namespace ProjectTime
{
    /// <summary>
    /// Summary description for DisplayStatus.
    /// </summary>
    public class DisplayStatus : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.DataGrid DGrdStatus;

        private void Page_Load(object sender, System.EventArgs e)
        {
                if(!Page.IsPostBack)
                BindDGrdStatus();

                DGrdStatus.EditCommand +=
                new DataGridCommandEventHandler(this.DGrdStatus_Edit);
            DGrdStatus.CancelCommand +=
                new DataGridCommandEventHandler(this.DGrdStatus_Cancel );
            DGrdStatus.UpdateCommand +=
                new DataGridCommandEventHandler(this.DGrdStatus_Update );
            }

        public void DGrdStatus_Edit(Object sender, DataGridCommandEventArgs e)
        {
            DGrdStatus.EditItemIndex = e.Item.ItemIndex;
            BindDGrdStatus();
        }

        public void DGrdStatus_Cancel(Object sender, DataGridCommandEventArgs e)
        {
            DGrdStatus.EditItemIndex = -1;
            BindDGrdStatus();
        }

        public void DGrdStatus_Update(Object sender, DataGridCommandEventArgs e)
        {

            string ConnectionString = @"User ID=sa;Initial Catalog=ProjectStatus;Data Source=(local);";
            if(ConnectionString == "")
            {
            ConnectionString = "Initial Catalog=ProjectStatus;Data Source=(local);Integrated Security=SSPI;";
            }
            SqlConnection cnn = new SqlConnection(ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Info",cnn);

            DataSet ds = new DataSet();
            DGrdStatus.DataSource = ds;
            DGrdStatus.DataMember = "Info";
            DGrdStatus.DataBind();
            SqlCommand upCmd = new SqlCommand(
                "UPDATE Info SET Total=@Total WHERE Num=@Num",cnn);
            upCmd.Parameters.Add("@Total", SqlDbType.NChar, 18, "Total");
            da.UpdateCommand = upCmd;
            da.Update(ds,"Info");

                        DGrdStatus.EditItemIndex = -1;
            BindDGrdStatus();
        }
        private void BindDGrdStatus()
        {
            string ConnectionString = @"User ID=sa;Initial Catalog=Project;Data Source=(local);";
            SqlConnection cnn = new SqlConnection(ConnectionString);
            SqlDataAdapter da = new SqlDataAdapter("SELECT * FROM Info",cnn);

            DataSet ds = new DataSet();
            da.Fill(ds, "Info");
            DGrdStatus.DataSource = ds;
            DGrdStatus.DataMember = "Info";
            DGrdStatus.DataBind();
        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {

            InitializeComponent();
            base.OnInit(e);
        }

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {


        }
        #endregion

        }
}
__________________
*******(*)*******
 
Old August 21st, 2004, 05:31 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 2,480
Thanks: 0
Thanked 1 Time in 1 Post
Default

Hi there,

This post should have gone into a .Net forum here.

Go to p2p.wrox.com homepage and expand DOTNET category and select the best fit forum from there and post this for better solution. This is a classic ASP databases forum. I don't think you would get better response for your post from here.

_________________________
- Vijay G
Strive for Perfection
 
Old August 22nd, 2004, 07:48 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

You're aren't getting the values from the datagrid. The DataGrid doesn't do those automatically. Get the values either by:

e.Item.Cells(<index>).Text

Or

e.Item.Cells(<index>).FindControl.<value>

Brian
 
Old August 22nd, 2004, 07:49 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Oh, I probably should have said that you get the values, pass them to the command by specifying the parameters value property, and then execute the ExecuteNonQuery of the UpdateCommand (I don't think Update will work that way).

Brian
 
Old August 23rd, 2004, 12:48 PM
Authorized User
 
Join Date: Aug 2004
Posts: 95
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via AIM to Arsi
Default

Oh! Ok! Thank you for your reply, I really appreciate it. I will try this.

*******(*)*******





Similar Threads
Thread Thread Starter Forum Replies Last Post
Updating SQL Database using Windows Forms in VB planopeeks VB Databases Basics 5 June 12th, 2008 11:33 AM
Updating to a SQL Database Arsi C# 3 August 23rd, 2004 12:49 PM
Updating database using Adapter for SQL Server syedjavid Biztalk 1 October 30th, 2003 05:37 AM





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