Parth Doshi
Parth Doshi

Reputation: 4208

Unable to display dataset on WinForm application

My problem is that I am unable to view the DataSet on my client side windows form. I am using the SetDataBinding method but I get an error saying that:

Error: "Systems.Windows.Forms.DataGridView" doesn't contain any defination for SetDataBinding and no extension method accepting first type of argument"

Here is web service code that returns Dataset:

  public class Service1 : System.Web.Services.WebService
  {
    [WebMethod]
    public DataSet getdata(String rollno)
    {
        try
        {
            using (SqlConnection myConnection = new SqlConnection(@"Data Source=.\SQLEXPRESS;Initial Catalog=student;User ID=sa;Password=123"))
            {
                string select = "select * from checkrecord where rollno=@rollno";
                SqlDataAdapter da = new SqlDataAdapter(select, myConnection);
                DataSet ds = new DataSet();
                da.Fill(ds, "students");
                return (ds);
            }
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
            return null;                
        }

    }

The client side code that binds the data:

      using System.Drawing;
      using System.Linq;
      using System.Text;
      using System.Windows.Forms;
      using System.Web.Services;
      using System.Web.Services.Protocols;
      using WindowsFormsApplication1.dataset1;

     //dataset1 is my web reference name
    namespace WindowsFormsApplication1
    {
      public partial class Form1 : Form
      { 
          public Form1()
          {
              InitializeComponent();
          }
          private void calldatagrid_Click(object sender, EventArgs e)
          {
             Service1 MyService = new Service1();
            // Call the GetData method in the Web Service
            DataSet ds = MyService.getdata("abc");
            // Bind the DataSet to the Grid
            datagrid.SetDataBinding=ds;
          }
      }
   }

Upvotes: 1

Views: 2133

Answers (2)

Priyank
Priyank

Reputation: 1174


change datagrid.SetDataBinding=ds; line to datagrid.DataSource=ds.Table[0];

use below piece of code:

 Service1 MyService = new Service1();
 // Call the GetData method in the Web Service
  DataSet ds = MyService.getdata("abc");
 // Bind the DataSet to the Grid
  datagrid.DataSource=ds.Table[0];


another change:
in your webservice change "select * from checkrecord where rollno=@rollno"; this line to "select * from checkrecord where rollno=\'"+rollno+"\'";

Upvotes: 2

Marc Gravell
Marc Gravell

Reputation: 1063864

SetDataBinding is defined on DataGrid, not DataGridView (unrelated), and is a method not a property, and required two parameters. Try instead:

datagrid.DataSource = ds;

And optionally:

datagrid.DataMember = "TableName";

As an aside... Data-sets are a horrible horrible choice for web-services.

Upvotes: 2

Related Questions