Hi guys,
I have problem with my login script. I am using VB.Net (ASP.Net) to do that things.
Here is the code:

This is the code for Default.aspx page

User Name: <asp:TextBox ID="userID" class="txtbox" runat="server"></asp:TextBox>
Password: <asp:TextBox ID="password" class="pwdbox" runat="server" TextMode="Password"></asp:TextBox>
<asp:LinkButton ID="login" class="button" runat="server">&nbsp;Login&nbsp;</asp:LinkButton>

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mcisaaConnectionString1 %>" ProviderName="<%$ConnectionStrings:mcisaaConnectionString1.ProviderName %>" SelectCommand="SELECT [UsrName], [Pass], [UsrStatus] FROM [UsrLoginInfo] WHERE (([UsrName] = @UsrName) AND ([Pass] = @Pass))">

  <SelectParameters>
    <asp:ControlParameter ControlID="userID" Name="UsrName" PropertyName="Text" Type="String"/>
    <asp:ControlParameter ControlID="password" Name="Pass" PropertyName="Text" Type="String"/>
  </SelectParameters>
</asp:SqlDataSource>

This is the code for Default.aspx.vb

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage

    Protected Sub login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles login.Click
        Dim xReader As Data.IDataReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)
        If xReader.Read Then
            Session("SessionStart") = "True"
            Session("UserID") = userID.Text
            Session("UserName") = xReader.Item("UsrName")
            Session("UserStatus") = xReader.Item("UsrStatus")
        End If
    End Sub
End Class

And I have no change for the web.config file.

While I am executed the page, and clicking login button, always show the error: Unable to cast object of type 'System.Data.DataView' to type 'System.Data.IDataReader'.

What's wrong with the code? I have try to finding solution on the internet but it was the same.

Thanks.

Recommended Answers

All 6 Replies

I found a snippet that converts the SqlDataSource into a DataView using CType as shown. Maybe you need to do something similar with your reader:

Dim dv As DataView
    Dim reorderedProducts As Integer

    dv = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), DataView)
    reorderedProducts = CType(dv.Table.Rows(0)(0), Integer)

I hope it helps! :)

Hi guys,
I have problem with my login script. I am using VB.Net (ASP.Net) to do that things.
Here is the code:

This is the code for Default.aspx page

User Name: <asp:TextBox ID="userID" class="txtbox" runat="server"></asp:TextBox>
Password: <asp:TextBox ID="password" class="pwdbox" runat="server" TextMode="Password"></asp:TextBox>
<asp:LinkButton ID="login" class="button" runat="server">&nbsp;Login&nbsp;</asp:LinkButton>

  <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:mcisaaConnectionString1 %>" ProviderName="<%$ConnectionStrings:mcisaaConnectionString1.ProviderName %>" SelectCommand="SELECT [UsrName], [Pass], [UsrStatus] FROM [UsrLoginInfo] WHERE (([UsrName] = @UsrName) AND ([Pass] = @Pass))">

  <SelectParameters>
    <asp:ControlParameter ControlID="userID" Name="UsrName" PropertyName="Text" Type="String"/>
    <asp:ControlParameter ControlID="password" Name="Pass" PropertyName="Text" Type="String"/>
  </SelectParameters>
</asp:SqlDataSource>

This is the code for Default.aspx.vb

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage

    Protected Sub login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles login.Click
        Dim xReader As Data.IDataReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)
        If xReader.Read Then
            Session("SessionStart") = "True"
            Session("UserID") = userID.Text
            Session("UserName") = xReader.Item("UsrName")
            Session("UserStatus") = xReader.Item("UsrStatus")
        End If
    End Sub
End Class

And I have no change for the web.config file.

While I am executed the page, and clicking login button, always show the error: Unable to cast object of type 'System.Data.DataView' to type 'System.Data.IDataReader'.

What's wrong with the code? I have try to finding solution on the internet but it was the same.

Thanks.

@gbeltrao: Thanks for info. But it still can't solved the problem. Because as shown in my code, .Read is not the member of DataView. While I run the app, it's error. I guess it's wrong at this part:

Dim xReader As Data.IDataReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

While I get apart of it, :

Dim xReader As Data.IDataReader
xReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

The debugger showed that the error is in line 2. Any suggestion please?
Thanks

@mrjimoy_05: Try converting the SqlDataSource to IDataReader:

Partial Class MasterPage
    Inherits System.Web.UI.MasterPage
    Protected Sub login_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles login.Click
        Dim xReader As Data.IDataReader = CType(SqlDataSource1.Select(DataSourceSelectArguments.Empty), IDataReader)
        If xReader.Read Then
            Session("SessionStart") = "True"
            Session("UserID") = userID.Text
            Session("UserName") = xReader.Item("UsrName")
            Session("UserStatus") = xReader.Item("UsrStatus")
        End If
    End Sub
End Class

I adapted your code based on what I found at MSN. Please let me know if it worked out (or not!) for you. :)

@gbeltrao: Thanks for info. But it still can't solved the problem. Because as shown in my code, .Read is not the member of DataView. While I run the app, it's error. I guess it's wrong at this part:

Dim xReader As Data.IDataReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

While I get apart of it, :

Dim xReader As Data.IDataReader
xReader = SqlDataSource1.Select(DataSourceSelectArguments.Empty)

The debugger showed that the error is in line 2. Any suggestion please?
Thanks

It still doesn't work. I tried to convert the SqlDataSource to IDataReader.

It's strange... Does jQuery work within the ASP.Net? Because I include jQuery on the page. I wonder if that's the problem. But after have a try, it still can't. Btw, I adopt the code from my last project.

The problem is that the SqlDataSource Select method will return a dataview if the DataSourceMode of the SqlDataSource is set to System.Web.UI.WebControls.SsqlDataSourceMode.DataSet.

Try changing the SqlDataSourceMode to System.Web.UI.WebControls.SqlDataSourceMode.DataReader and that should fix your problem.

Hey, that's fixed the problem, @DhCoder! Thank you so much. ^^

@gbeltrao: Thanks for the help! :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.