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

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.

Dani AI

Generated

A common root cause for the cast error is that SqlDataSource.Select returned a DataView while the code expected an IDataReader. The SqlDataSource control defaults to a disconnected DataSet mode (which yields a DataView from Select), so a direct cast to IDataReader fails. 's suggestion to change the SqlDataSource mode is the correct corrective action; 's DataView approach is the right alternative when a disconnected result set is preferred.

Set the SqlDataSource to return a reader (example shows only the relevant attribute):

<asp:SqlDataSource ID="SqlDataSource1" runat="server"
    DataSourceMode="DataReader"
    ... />

Notes and practical tips:

  • DataReader vs DataSet: a DataReader is forward-only and keeps the DB connection open while reading; it is efficient for simple reads but not suitable for scenarios that need a disconnected result or random access. A DataSet/DataView is disconnected and better for data binding or multiple passes over rows.
  • Resource management: when using a DataReader result, ensure it is closed/disposed promptly (wrap it in a Using block or call Close) to avoid leaking connections.
  • If leaving the control in DataSet mode, read fields from the returned DataView (as suggested by ) instead of attempting an IDataReader cast.

Production cautions: do not store plain-text passwords; use proper salted hashing and an authentication framework rather than raw SQL+session-based checks. Minimal session data should be stored and sensitive values avoided. These steps explain the cast failure and place the chosen fix in safer, more maintainable practice.

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

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.

: 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

: 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 . 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, ! Thank you so much. ^^

: 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.