With the ASP.NET Web matrix Project, I'm working through the example code for Sams "Teach Yourself ASP.NET in 24 Hours", but I'm running into an error that I can't solve. For the "Allowing the User to Decide What Customer's Information to View" section of the book's Chapter 16, I typed the following code:
Code:
<%@ Page Language="VB" %>
<script runat="server">
Function faqs() As System.Data.DataSet
Dim connectionString As String = "server='SERVERNAME'; user id='UN'; password='PW'; database='DBNAME'"
Dim dbConnection As System.Data.IDbConnection = New System.Data.SqlClient.SqlConnection(connectionString)
Dim queryString As String = "SELECT * FROM [FAQ]"
Dim dbCommand As System.Data.IDbCommand = New System.Data.SqlClient.SqlCommand
dbCommand.CommandText = queryString
dbCommand.Connection = dbConnection
Dim dataAdapter As System.Data.IDbDataAdapter = New System.Data.SqlClient.SqlDataAdapter
dataAdapter.SelectCommand = dbCommand
Dim dataSet As System.Data.DataSet = New System.Data.DataSet
dataAdapter.Fill(dataSet)
Return dataSet
End Function
Sub btnView_Click(sender As Object, e As EventArgs)
dgfaqs.DataSource = faqs(FAQID.Text)
dgfaqs.DataBind()
If dgfaqs.Items.Count = 0 then
lblNoRecords.Visible = True
Else
lblNoRecords.Visible = False
End If
End Sub
</script>
<html>
<head>
</head>
<body>
<form runat="server">
<p>
FAQ ID:
<asp:TextBox id="FAQID" runat="server" Columns="3" MaxLength="3" OnTextChanged="FAQID_TextChanged"></asp:TextBox>
</p>
<p>
<asp:Button id="btnView" onclick="btnView_Click" runat="server" Text="View Customer"></asp:Button>
</p>
<p>
<asp:DataGrid id="dgfaqs" runat="server" AutoGenerateColumns="False">
<AlternatingItemStyle font-size="10pt" font-names="Arial" backcolor="#FFFFCC"></AlternatingItemStyle>
<ItemStyle font-size="10pt" font-names="Arial"></ItemStyle>
<HeaderStyle font-size="12pt" font-names="Arial" font-bold="True" horizontalalign="Center" forecolor="White" backcolor="Navy"></HeaderStyle>
<Columns>
<asp:BoundColumn DataField="FAQ_ID" HeaderText="ID"></asp:BoundColumn>
<asp:BoundColumn DataField="Question" HeaderText="Questions"></asp:BoundColumn>
<asp:BoundColumn DataField="Answer" HeaderText="Answers"></asp:BoundColumn>
</Columns>
</asp:DataGrid>
</p>
<p>
<asp:Label id="lblNoRecords" runat="server" font-bold="True">There were no records</asp:Label>
</p>
<!-- Insert content here -->
</form>
</body>
</html>
...but this code results in this error message:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: BC30367: Class 'System.Data.DataSet' cannot be indexed because it has no default property.
Source Error:
Line 20:
Line 21: Sub btnView_Click(sender As Object, e As EventArgs)
Line 22: dgfaqs.DataSource = faqs(FAQID.Text)
Line 23: dgfaqs.DataBind()
Line 24:
I've tried looking up that error, but I've still not come up a solution. If someone can let me know what I'm doing wrong, and how I can fix it, it would be greatly appreciated. Thanks.
KWilliams
-----------------------
It's the end of the world as we know it...and I feel fine
CardboardHammer is right. You have the function faqs() as DataSet and you are trying to pass a parameter to the function, and because there is no parameter in your function, it is trying to index the DataSet and find the element in there.
You should modify the function faqs() to have a parameter so that you can use it as a parameter to your SELECT statement and return your data.
Ben Miller
All responses are provided AS IS and do not necessarily reflect my employers views.
Bookmarks