467,876 Members | 1,093 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 467,876 developers. It's quick & easy.

Data not binding to datalist

I am having difficulty binding data to my datalist. The data list does
not show. I have gone round and round for two days. I could really
use some help looking through this code and seeing where the error is.
It is usually something simple but I just can not find it!!!

Any suggestion?

--------------------------------------------------------------------------
My HTML code is:

<asp:DataList id="DataList1" runat="server">
<HeaderTemplate>
This is the header
</HeaderTemplate>
<FooterTemplate>
This is the footer
</FooterTemplate>
<ItemTemplate>
<img src='../images/items/thumbes/<%#
DataBinder.Eval(Container.DataItem, "strImagePath") %>'>

This is a test
</ItemTemplate>
</asp:DataList>
------------------------------------------------------------------------------------
Code behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BrandID As String = Request.QueryString("BrandID")
Dim CategoryID As String = Request.QueryString("CategoryID")
Dim strSubCategory As String =
Request.QueryString("StrSubCategory")

If Not CategoryID Is Nothing Then
DataList1.DataSource =
Catalog.GetSubCatProductsFromBrands(CategoryID, strSubCategory)
DataList1.DataBind()
Label.Text = "Category"
ElseIf Not BrandID Is Nothing Then
DataList1.DataSource =
Catalog.GetSubCatProductsFromBrands(strSubCategory , BrandID)
DataList1.DataBind()
Label.Text = "brands"

End If
End Sub

---------------------------------------------------------------------------
Datareader code in catalog class file

Public Shared Function GetSubCatProductsFromBrands(ByVal BrandID As
String, ByVal StrSubCategory As String) As SqlDataReader
'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New
SqlCommand("New_getSubCatProductsFromBrands", connection)
command.CommandType = CommandType.StoredProcedure

'Add an input parameter and suply a valiue for it
command.Parameters.Add("@brandID", SqlDbType.NVarChar, 5)
command.Parameters("@brandID").Value = BrandID

command.Parameters.Add("@StrSubCategory", SqlDbType.NVarChar,
50)
command.Parameters("@StrSubCategory").Value = StrSubCategory
Try
'open the connection
connection.Open()
'Return an SQLDataReader
Return
command.ExecuteReader(CommandBehavior.CloseConnect ion)
Catch e As Exception
'close the connection and throw the exception
connection.Close()
' Throw e
End Try
End Function

------------------------------------------------------------------
Stored proc is
( @BrandID NvarChar (5),
@StrSubCategory NvarChar (50)
)
As

Select intProductID, StrProductCode, StrProductName, curPrice,
strImagePath
from tblProducts
Where strBrandCode = @BrandID
and StrSubCategory = @strSubCategory
Return
HELP!!

TIA
Laura K

Dec 28 '05 #1
  • viewed: 1693
Share:
5 Replies
looks like you have your return statement ahead of Execute reader in the class file.

put the return statement after the execution.

---------------------------------------------------------------------------
Datareader code in catalog class file ...............
Try
'open the connection
connection.Open()
'Return an SQLDataReader
Return
command.ExecuteReader(CommandBehavior.CloseConnect ion)
Catch e As Exception
'close the connection and throw the exception
connection.Close()
' Throw e
End Try

...................
Dec 28 '05 #2
Actuall the line was broken up when the message posted. The following
text should all be on one line. I also use this text on other
datareaders and it works.

Return command.ExecuteReader(CommandBehavior.CloseConnect ion)

Dec 28 '05 #3
On Wed, 28 Dec 2005 12:49:48 -0800, Laura K wrote:
I am having difficulty binding data to my datalist. The data list does
not show. I have gone round and round for two days. I could really
use some help looking through this code and seeing where the error is.
It is usually something simple but I just can not find it!!!

Any suggestion?

--------------------------------------------------------------------------
My HTML code is:

<asp:DataList id="DataList1" runat="server">
<HeaderTemplate>
This is the header
</HeaderTemplate>
<FooterTemplate>
This is the footer
</FooterTemplate>
<ItemTemplate>
<img src='../images/items/thumbes/<%#
DataBinder.Eval(Container.DataItem, "strImagePath") %>'>

This is a test
</ItemTemplate>
</asp:DataList>
------------------------------------------------------------------------------------
Code behind
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load
Dim BrandID As String = Request.QueryString("BrandID")
Dim CategoryID As String = Request.QueryString("CategoryID")
Dim strSubCategory As String =
Request.QueryString("StrSubCategory")

If Not CategoryID Is Nothing Then
DataList1.DataSource =
Catalog.GetSubCatProductsFromBrands(CategoryID, strSubCategory)
DataList1.DataBind()
Label.Text = "Category"
ElseIf Not BrandID Is Nothing Then
DataList1.DataSource =
Catalog.GetSubCatProductsFromBrands(strSubCategory , BrandID)
DataList1.DataBind()
Label.Text = "brands"

End If
End Sub

---------------------------------------------------------------------------
Datareader code in catalog class file

Public Shared Function GetSubCatProductsFromBrands(ByVal BrandID As
String, ByVal StrSubCategory As String) As SqlDataReader
'create the connection string
Dim connection As New SqlConnection(connectionString)
'Create and initialize the command Object
Dim command As New
SqlCommand("New_getSubCatProductsFromBrands", connection)
command.CommandType = CommandType.StoredProcedure

'Add an input parameter and suply a valiue for it
command.Parameters.Add("@brandID", SqlDbType.NVarChar, 5)
command.Parameters("@brandID").Value = BrandID

command.Parameters.Add("@StrSubCategory", SqlDbType.NVarChar,
50)
command.Parameters("@StrSubCategory").Value = StrSubCategory
Try
'open the connection
connection.Open()
'Return an SQLDataReader
Return
command.ExecuteReader(CommandBehavior.CloseConnect ion)
Catch e As Exception
'close the connection and throw the exception
connection.Close()
' Throw e
End Try
End Function

------------------------------------------------------------------
Stored proc is
( @BrandID NvarChar (5),
@StrSubCategory NvarChar (50)
)
As

Select intProductID, StrProductCode, StrProductName, curPrice,
strImagePath
from tblProducts
Where strBrandCode = @BrandID
and StrSubCategory = @strSubCategory
Return
HELP!!

TIA
Laura K

Try to get the stuff to work from an array; data binding works with arrays
also.

Dec 28 '05 #4
Can you give me some direction for that. Are there any tutorials out
there or can you give me some sample code.
Also I have been experimenting and have found that my stored proc is
not even being executed (I tested it by turning off execute
privileges). So the data is not even being passed to the procedure.

Dec 28 '05 #5
Found my own error. I had commented out Throw e

Dec 29 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

1 post views Thread by .Net Sports | last post: by
1 post views Thread by Laura K | last post: by
3 posts views Thread by bbernieb | last post: by
2 posts views Thread by Andrew Robinson | last post: by
1 post views Thread by Aaron | last post: by
15 posts views Thread by mosipenko | last post: by
reply views Thread by AleXmanFree | last post: by
reply views Thread by jack112 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.