Wrox Programmer Forums
Go Back   Wrox Programmer Forums > C# and C > C# 1.0 > C#
|
C# Programming questions specific to the Microsoft C# language. See also the forum Beginning Visual C# to discuss that specific Wrox book and code.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C# 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:52 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 a 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:

using 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 22nd, 2004, 07:59 PM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,998
Thanks: 0
Thanked 3 Times in 3 Posts
Default

Hey,

Posted to your other thread.

Brian
 
Old August 23rd, 2004, 09:01 AM
Friend of Wrox
 
Join Date: Jun 2003
Posts: 1,101
Thanks: 0
Thanked 2 Times in 2 Posts
Default

DO NOT CROSS POST
http://p2p.wrox.com/topic.asp?TOPIC_ID=17934

Hal Levy
Web Developer, PDI Inc.

NOT a Wiley/Wrox Employee
 
Old August 23rd, 2004, 12:49 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

ok...sorry again

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





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 SQL Database Arsi Classic ASP Databases 4 August 23rd, 2004 12:48 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.