943,793 Members | Top Members by Rank

Ad:
  • ASP.NET Discussion Thread
  • Marked Solved
  • Views: 801
  • ASP.NET RSS
Jul 6th, 2009
0

Session error in asp net

Expand Post »
hi,

there , i am coming up with a login page and i am having error when i run the programme. It says 'Invalid attempt to read when no data is present'.

Session("ses_uname") = rdr("FirstName") + " " + rdr("LastName")


Imports System.Data.Sql
Imports System.Data.SqlClient
Imports System.Web
Partial Class _Default
    Inherits System.Web.UI.Page
    Dim str As String = ConfigurationManager.ConnectionStrings("CourierSystemConnectionString").ConnectionString
    Dim con As SqlConnection
    Dim cmd As SqlCommand
    Dim rdr As SqlDataReader
    Dim sql As String
    Dim t As String
    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
    End Sub
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnlogin.Click
        con = New SqlConnection(str)
        con.Open()
        Dim type As String
        type = ddltype.SelectedValue
        If type = "Customer" Then
            sql = "select * from CusRegis_Table where CusID='" + tuname.Text + "' and password2='" + tupass.Text + "'"

        Else
            sql = "select * from StaffRegis_Table where StaffID='" + tuname.Text + "' and Password='" + tupass.Text + "'"
        End If
        cmd = New SqlCommand(sql, con)
        rdr = cmd.ExecuteReader

        If rdr.HasRows Then
            If ddltype.SelectedValue = "Customer" Then
                Session("ses_dname") = rdr("CusFName") + " " + rdr("CusLName")
                Session("ses_uid") = rdr("Cusid")
                Response.Redirect("Home1.aspx")
                'ElseIf 
                '   ddltype.SelectedValue = "Delivery Boy" Then
                '  Session("ses_dname") = rdr("FirstName") + " " + rdr("Lastname")
                ' Session("ses_uid") = rdr("staffid")
                'Session("ses_utype") = rdr("Designation")
                'Response.Redirect("DeliveryBoyDirection.aspx")
            Else
                ddltype.SelectedValue = "DeliveryBoy"
                Session("ses_uname") = rdr("FirstName") + " " + rdr("LastName")                Session("ses_uid") = rdr("staffid")
                Session("ses_utype") = rdr("Designation")
                Response.Redirect("DeliveryBoyDirection.aspx")

            End If
        End If
    End Sub
End Class
Similar Threads
Reputation Points: 10
Solved Threads: 0
Light Poster
chrispaul8676 is offline Offline
36 posts
since Jul 2009
Jul 7th, 2009
0

Re: Session error in asp net

Include the read method first.

rdr.read();
if(rdr.hasrows)
..............................
Reputation Points: 26
Solved Threads: 44
Posting Whiz in Training
mail2saion is offline Offline
247 posts
since Apr 2009
Jul 8th, 2009
0

Re: Session error in asp net

thank you for replying.
I am not sure where to key in the read method
Reputation Points: 10
Solved Threads: 0
Light Poster
chrispaul8676 is offline Offline
36 posts
since Jul 2009
Jul 8th, 2009
0

Re: Session error in asp net

Just change 'If rdr.HasRows' to 'If rdr.Read()'

Therefore your code should be as follows
If rdr.Read() Then
            If ddltype.SelectedValue = "Customer" Then
                Session("ses_dname") = rdr("CusFName") + " " + rdr("CusLName")
                Session("ses_uid") = rdr("Cusid")
                Response.Redirect("Home1.aspx")
                'ElseIf 
                '   ddltype.SelectedValue = "Delivery Boy" Then
                '  Session("ses_dname") = rdr("FirstName") + " " + rdr("Lastname")
                ' Session("ses_uid") = rdr("staffid")
                'Session("ses_utype") = rdr("Designation")
                'Response.Redirect("DeliveryBoyDirection.aspx")
            Else
                ddltype.SelectedValue = "DeliveryBoy"
                Session("ses_uname") = rdr("FirstName") + " " + rdr("LastName")                Session("ses_uid") = rdr("staffid")
                Session("ses_utype") = rdr("Designation")
                Response.Redirect("DeliveryBoyDirection.aspx")

            End If
        End If
Last edited by Ramesh S; Jul 8th, 2009 at 3:05 am.
Reputation Points: 165
Solved Threads: 113
Posting Pro
Ramesh S is offline Offline
580 posts
since Jun 2009
Jul 10th, 2009
0

Re: Session error in asp net

i think you are VB.net...but i'm the C# guy...

i guess you are missing toSting() function which is required...

in your code instead storing the string,you are storing the reader object...

ASP.NET Syntax (Toggle Plain Text)
  1. string finaltext = rdr["FirstName"].tostring() + "" + rdr["LastName"].toString();
  2.  
  3. Session.add("ses_uname",) = rdr("FirstName") + " " + rdr("LastName")

hope it helps...and if i'm wrong please let me know...
Last edited by peter_budo; Jul 10th, 2009 at 6:26 am. Reason: Keep It Organized - For easy readability, always wrap programming code within posts in [code] (code blocks) and [icode] (inline code) tags.
Reputation Points: 28
Solved Threads: 106
Banned
dnanetwork is offline Offline
633 posts
since May 2008

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in ASP.NET Forum Timeline: How to check if page has been modified?
Next Thread in ASP.NET Forum Timeline: creating data entry form





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC