Go Premium for a chance to win a PS4. Enter to Win

x
?
Solved

properties not available in strongly typed dataset

Posted on 2009-04-02
4
Medium Priority
?
687 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 2000 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Introduction This article shows how to use the open source plupload control to upload multiple images. The images are resized on the client side before uploading and the upload is done in chunks. Background I had to provide a way for user…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an anti-spam), the admin…
Is your OST file inaccessible, Need to transfer OST file from one computer to another? Want to convert OST file to PST? If the answer to any of the above question is yes, then look no further. With the help of Stellar OST to PST Converter, you can e…
Suggested Courses

824 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