Solved

properties not available in strongly typed dataset

Posted on 2009-04-02
4
653 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-
  • 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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Today is the age of broadband.  More and more people are going this route determined to experience the web and it’s multitude of services as quickly and painlessly as possible. Coupled with the move to broadband, people are experiencing the web via …
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.

810 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