0

Hey everyone. I'm trying to get a dataset to a page from a web service and im having problems. I have been using the MS page here: http://support.microsoft.com/kb/310143 and copied it exactly(o so i thought). And for some reason my datagrid (DataGrid1 in my program) is not being populated and shown when Load is clicked. Below is my code. Can anyone let me know where I went wrong? Thanks.

PS: My webservice compiles fine and will generate the XML for when u invoke GetCustomers, but like i said above, the datagrid is not populated in the web app that consumes it. Any help would be apprecaited.

WebService:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
using System.Diagnostics;
using System.Web;
using System.Web.Services;

namespace DBService
{
    /// <summary>
    /// Summary description for Service1.
    /// </summary>
    public class Service1 : System.Web.Services.WebService
    {
        public Service1()
        {
            InitializeComponent();
        }

        #region Component Designer generated code
        
        //Required by the Web Services Designer 
        private IContainer components = null;
                
        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        private void InitializeComponent()
        {
        }

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        protected override void Dispose( bool disposing )
        {
            if(disposing && components != null)
            {
                components.Dispose();
            }
            base.Dispose(disposing);        
        }
        
        #endregion

        [WebMethod]
        public DataSet GetCustomers()
        {
            SqlConnection con = new SqlConnection("server=(local);" + 
                "Trusted_Connection=yes;" + 
                "database=Northwind;" +  
                "connection timeout=30");
            
            SqlDataAdapter daCust = new SqlDataAdapter("Select * From Customers", con);
            DataSet ds = new DataSet();
            daCust.Fill(ds, "Cust");
            return ds;
        }

        [WebMethod]
        public DataSet UpdateCustomers(DataSet ds)
        {
            SqlConnection con = new SqlConnection("server=(local);" + 
                "Trusted_Connection=yes;" + 
                "database=Northwind;" +  
                "connection timeout=30");
            
            SqlDataAdapter daCust = new SqlDataAdapter("Select * From Customers", con);
            SqlCommandBuilder cbCust = new SqlCommandBuilder(daCust);
            daCust.Update(ds, "Cust");
            return ds;
        }
    }
}

WebApp:

using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Data.SqlClient;
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 GetDataThroughService
{
    /// <summary>
    /// Summary description for WebForm1.
    /// </summary>
    public class WebForm1 : System.Web.UI.Page
    {
        protected System.Web.UI.WebControls.Button Button1;
        protected System.Web.UI.WebControls.Button Button2;
        protected System.Web.UI.WebControls.DataGrid DataGrid1;
    
        private void Page_Load(object sender, System.EventArgs e)
        {

        }

        #region Web Form Designer generated code
        override protected void OnInit(EventArgs e)
        {
            //
            // CODEGEN: This call is required by the ASP.NET Web Form Designer.
            //
            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()
        { 
            this.Button1.Click += new System.EventHandler(this.Button1_Click);
            this.Button2.Click += new System.EventHandler(this.Button2_Click);
            this.Load += new System.EventHandler(this.Page_Load);

        }
        #endregion

        private void Button1_Click(object sender, System.EventArgs e)
        {
            localhost.Service1 MyService = new localhost.Service1();
            DataGrid1.DataSource = MyService.GetCustomers();
            DataGrid1.DataMember = "Cust";
        }

        private void Button2_Click(object sender, System.EventArgs e)
        {
            localhost.Service1 MyService = new localhost.Service1();
            DataSet ds = (DataSet) DataGrid1.DataSource;
            DataSet dsChanges = ds.GetChanges();
            if (dsChanges != null)
            {
                ds.Merge(MyService.UpdateCustomers(dsChanges), true);
            }
        }
    }
}
1
Contributor
1
Reply
2
Views
10 Years
Discussion Span
Last Post by Barefootsanders
0

Also, if anyone wants to tell me why i get, "Object reference not set to an instance of an object." when I click save on the web app it would be great:)

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.