Solved

properties not available in strongly typed dataset

Posted on 2009-04-02
4
675 Views
Last Modified: 2012-05-06
I have created a simple strongly typed dataset and filled the datatable with the authors table from the pubs database and binded to a gridview.

I'm not able to access the properties of the strongly typed dataset in the button click event handler.

What am I doing wrong?

See the code below.

Please note, this is just a example I'm performing to learn and understand the differences between typed and untyped datasets.

Thanks for any help.
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 StronglyTypedDataSetExample
{
    public partial class _Default : System.Web.UI.Page
    {
        // connection object to connect to the pubs database
        SqlConnection con = new SqlConnection();
        // instantiate command object
        SqlCommand cmd = new SqlCommand();
        // instantiate the data adapter object
        SqlDataAdapter daAuthors = new SqlDataAdapter();
         // instantiate the strongly typed dataset I manually created
        dsAuthors ds = new dsAuthors();
        // instantiate the authorts datatable in the strongly typed dataset I created
        dsAuthors.AuthorsDataTable dt = new dsAuthors.AuthorsDataTable();
 
        protected void Page_Load(object sender, EventArgs e)
        {
           // set the connection string
            con.ConnectionString = "data source=DEPPS\\SQLExpress;initial catalog=pubs;user id=xxxxxxxx;password=xxxxxxxxxx4;persist security info=True";
 
            cmd.CommandTimeout = 60;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM authors";
            cmd.Connection = con;
 
            con.Open();
 
            daAuthors.SelectCommand = cmd;
           // fill the data table
            daAuthors.Fill(dt);
 
            con.Close();
 
            gvAuthors.DataSource = dt;  // bind using the datatable
            gvAuthors.DataBind();
        }
 
        protected void btnAuthor_Click(object sender, EventArgs e)
        {
            //Response.Write("Last Name is: " + dt.Rows[0].the properties are not available?????
        }
    }
}

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
  • 2
4 Comments
 
LVL 16

Expert Comment

by:burakiewicz
ID: 24052491
you can do it 1 of two ways
((dsAuthors..AuthorsDataRow)dt.Rows[0]). whatever property and it should come up in intellisense
or
dsAuthors..AuthorsDataRow adr=dt.Rows[0];
adr. whatever property and it should come up in intellisense
0
 

Author Comment

by:-Dman100-
ID: 24053268
Hi burakiewicz,

Thanks for thet help.

Okay, that did work.  If I can ask one follow-up question.  When I created an untyped dataset, I used the following code:

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 LooselyTypedDataSetExample
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter daAuthors = new SqlDataAdapter();
        DataSet dsAuthors = new DataSet();
        DataTable dt = new DataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "data source=DEPPS\\SQLExpress;initial catalog=pubs;user id=dwayne;password=Z1x2c3v4;persist security info=True";

            cmd.CommandTimeout = 60;
            cmd.CommandText = "SELECT * FROM authors";
            cmd.CommandType = CommandType.Text;
            cmd.Connection = con;

            con.Open();

            daAuthors.SelectCommand = cmd;
            daAuthors.Fill(dsAuthors, "authors");

            con.Close();

            GridView1.DataSource = dsAuthors.Tables["authors"];
            GridView1.DataBind();
        }

        protected void btnAuthor_Click(object sender, EventArgs e)
        {
            dt = dsAuthors.Tables["authors"];
            Response.Write("Last Name is: " + dt.Rows[0]["au_lname"].ToString());
        }
    }
}

Creating a strongly typed dataset had the following code:

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 StronglyTypedDataSetExample
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter daAuthors = new SqlDataAdapter();
        dsAuthors.AuthorsDataTable dt = new dsAuthors.AuthorsDataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "data source=DEPPS\\SQLExpress;initial catalog=pubs;user id=dwayne;password=Z1x2c3v4;persist security info=True";

            cmd.CommandTimeout = 60;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM authors";
            cmd.Connection = con;

            con.Open();

            daAuthors.SelectCommand = cmd;
            daAuthors.Fill(dt);

            con.Close();

            gvAuthors.DataSource = dt;
            gvAuthors.DataBind();
        }

        protected void btnAuthor_Click(object sender, EventArgs e)
        {
            Response.Write("Last Name is: " + ((dsAuthors.AuthorsRow)dt.Rows[0]).au_lname);
        }
    }
}

Is is possible to use similar code as shown the untyped dataset code with a typed dataset...i.e:


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 StronglyTypedDataSetExample
{
    public partial class _Default : System.Web.UI.Page
    {
        SqlConnection con = new SqlConnection();
        SqlCommand cmd = new SqlCommand();
        SqlDataAdapter daAuthors = new SqlDataAdapter();
        dsAuthors ds = new dsAuthors();
        dsAuthors.AuthorsDataTable dt = new dsAuthors.AuthorsDataTable();

        protected void Page_Load(object sender, EventArgs e)
        {
            con.ConnectionString = "data source=DEPPS\\SQLExpress;initial catalog=pubs;user id=dwayne;password=Z1x2c3v4;persist security info=True";

            cmd.CommandTimeout = 60;
            cmd.CommandType = CommandType.Text;
            cmd.CommandText = "SELECT * FROM authors";
            cmd.Connection = con;

            con.Open();

            daAuthors.SelectCommand = cmd;
            daAuthors.Fill(ds, "Authors");

            con.Close();

            gvAuthors.DataSource = ds;
            gvAuthors.DataBind();
        }

        protected void btnAuthor_Click(object sender, EventArgs e)
        {
            Response.Write("Last Name is: " + ((dsAuthors.AuthorsRow)dt.Rows[0]).au_lname);
        }
    }
}


I get the error: There is no row at position 0.

When called the data adapter fill method, I'm passing the dsAuthors typed dataset and the name of the datatable.  The gridview is populated, but why can't I access the column the same?  Does it require a different approach?

Thanks for your help.
Regards.
0
 
LVL 16

Accepted Solution

by:
burakiewicz earned 500 total points
ID: 24053646
looks like with your code you are not filling dt but ds so try this.
 Response.Write("Last Name is: " + ((dsAuthors.AuthorsRow)ds.Tables["Authors"].Rows[0]).au_lname)
are you trying to do something like that
or
Response.Write("Last Name is: " + ((dsAuthors.AuthorsRow)ds.AuthorsDataTable.Rows[0]).au_lname);
or
Response.Write("Last Name is: " + ds.AuthorsDataTable.Rows[0]["au_lname"].ToString());
0
 

Author Comment

by:-Dman100-
ID: 24054020
yes, thank you.  That helped a lot and makes much more sense.  I appreciate your help.
0

Featured Post

Ready to get started with anonymous questions?

It's easy! Check out this step-by-step guide for asking an anonymous question on Experts Exchange.

Question has a verified solution.

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

In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
In this video, viewers are given an introduction to using the Windows 10 Snipping Tool, how to quickly locate it when it's needed and also how make it always available with a single click of a mouse button, by pinning it to the Desktop Task Bar. Int…
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…

627 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