Solved

properties not available in strongly typed dataset

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

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick way to get a menu to work on our website, is using the Menu control and assign it to a web.sitemap using SiteMapDataSource. Example of web.sitemap file: (CODE) Sample code to add to the page menu: (CODE) Running the application, we wi…
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…
Established in 1997, Technology Architects has become one of the most reputable technology solutions companies in the country. TA have been providing businesses with cost effective state-of-the-art solutions and unparalleled service that is designed…
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…

820 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