 |
| ASP.NET 1.1 As of 10/6/2005, this forum is locked as part of the reorganization described here: http://p2p.wrox.com/topic.asp?TOPIC_ID=35394. No posts have been deleted. Open ongoing discussions from the last week have been moved to either ASP.NET 1.0 and 1.1 Beginners http://p2p.wrox.com/asp-net-1-0-1-1-basics-60/ or ASP.NET 1.0 and 1.1 Professional. http://p2p.wrox.com/forum.asp?FORUM_ID=50. See my sticky post inside for more. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the ASP.NET 1.1 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

May 11th, 2005, 06:39 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
specified cast is not valid
Hello Friends,
i am writing a program where in i am trying to display dataset in 3 ways on the web page.
1) using a datalist.
2)using a variable.
3)using a datagrid.
the code is as follows:
private void Page_Load(object sender, System.EventArgs e)
{
if(this.IsPostBack==false)
{
try
{
SqlConnection myconn= new SqlConnection(strconn);
string mySql="select insertdata.fname, insertdata.lname,
insertdata.************, insertdata.position from insertdata";
DataSet Ds= new DataSet();
SqlDataAdapter Adinsert=new SqlDataAdapter(mySql, myconn);
Adinsert.Fill(Ds, "insertdata");
//The next 2 lines assign the data in the dataset to Datalist
datalist.DataSource=Ds.Tables["insertdata"].DefaultView;
datalist.DataBind();
//These lines assign the data to a variable and again
//assign it to the text property of literal control
foreach(DataRow dr in Ds.Tables)
{
sEmp += "<b>" + dr["firstname"] + " " + dr["lastname"]
+ "</b> - " + dr["************"] + "" + dr["position"] + "<br>" ;
}
Literalprg.Text = sEmp;
//then, here we assign the data to a DataGrid,
//much like the way we did in the example for the DataList dginsertdata.Datasource=Ds.Tables
["insertdata"].DefaultView ;
dginsertdata.DataBind();
} //try
catch(SqlException ex)
{
lblexec.Text =ex.Message;
}
catch(Exception ex)
{
lblexec.Text =ex.Message;
}
} //if
} //pageload
now, i am getting error at 2 places right now :
1) in the foreach loop ====> Specified cast is not valid
2) the following line where i am using datagrid :
dginsertdata.Datasource=Ds.Tables["insertdata"].DefaultView ;
and the error is ===>
'System.Web.UI.WebControls.DataGrid' does not contain a definition for 'Datasource'
cud anyone plz help me out.
thanx and regards,
muskaan. :)
|
|

May 11th, 2005, 07:14 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi,
'dginsertdata.Datasource' should be 'dginsertdata.DataSource'
Prashant
|
|

May 11th, 2005, 07:16 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
HI,
I think you got It, 'S' should be caps
Prashant
|
|

May 11th, 2005, 07:17 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi,
Sorry I missed the first one
foreach(DataRow dr in Ds.Tables)
should be
"foreach(DataRow dr in Ds.Tables[0].Rows)"
|
|

May 11th, 2005, 08:08 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks a lot prashant.
now i the program is displaying dataset using datagrid and variable.
but not displaying dataset using datalist.
i dont know whts the prb.
cud u plz help me out.
thanks and regards,
muskaan.
|
|

May 11th, 2005, 11:26 PM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi,
The datalist does not has a an autogenerate freature as the datagrid, so you must create the nessary templates/columns...
Check whether you have done this.
Prashant
|
|

May 12th, 2005, 02:01 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi prashant,
thanx but cud u plz tell me how do i create it?
muskaan.
|
|

May 12th, 2005, 02:24 AM
|
|
Friend of Wrox
|
|
Join Date: Apr 2005
Posts: 186
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
hi,
See the example in MSDN
Code:
<asp:DataList id="ItemsList"
BorderColor="black"
CellPadding="5"
CellSpacing="5"
RepeatDirection="Vertical"
RepeatLayout="Table"
RepeatColumns="3"
ShowBorder="True"
runat="server">
<HeaderStyle BackColor="#aaaadd">
</HeaderStyle>
<AlternatingItemStyle BackColor="Gainsboro">
</AlternatingItemStyle>
<HeaderTemplate>
List of items
</HeaderTemplate>
<ItemTemplate>
Description: <br>
<%# DataBinder.Eval(Container.DataItem, "StringValue") %>
<br>
Price: <%# DataBinder.Eval(Container.DataItem, "CurrencyValue", "{0:c}") %>
<br>
<asp:Image id="ProductImage"
ImageUrl='<%# DataBinder.Eval(Container.DataItem, "ImageValue") %>'
runat="server"/>
</ItemTemplate>
</asp:DataList>
you can get mmore info from
http://msdn.microsoft.com/library/de...ClassTopic.asp
|
|

May 12th, 2005, 04:14 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Thanks Prashant.
i'll do that.
and incase of any prb, i'll let u know.
Regards,
Muskaan. :)
|
|

May 12th, 2005, 05:20 AM
|
|
Authorized User
|
|
Join Date: May 2005
Posts: 92
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi Prashant,
i am sorry to trouble to u again.
But am not getting how to use the datalist.
DO i have to drag and drop the controls over it?
Or do i have do that thru coding?
or thru the HTML code?
Plz explain with a small example.
Thanks and regards,
Muskaan.
|
|
 |