Solved

properties not available in strongly typed dataset

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
IntroductionWhile developing web applications, a single page might contain many regions and each region might contain many number of controls with the capability to perform  postback. Many times you might need to perform some action on an ASP.NET po…
This video discusses moving either the default database or any database to a new volume.
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

757 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now