473,663 Members | 2,933 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Problem with DataReader

44 New Member
Hi friends
This is sweatha, fresher MCA 2007 batch. Right now I am working in asp.net platform.
My dillema is that I have created a registration form with the fields as username & password. And if once the user register the username and password, it will be stored in the database(SQL Server 2000) by showing "Records Inserted".

Then I have created the login form just like the fields as username and password.And if the user enters the values it should check in the database & it should display whether the data is valid or invalid. Here, I have written the coding but the problem is with the datareader. If I run the page it is showing the error as "c:\inetpub\www root\extra1\Log in.aspx.vb(45): 'System.Data.Sq lClient.SqlData Reader.Private Sub New(command As System.Data.Sql Client.SqlComma nd)' is not accessible in this context because it is 'Private'." And also if I run the page instead of displaying valid or invalid user, It is displaying as "records inserted" which I have given in the registration page.

The Coding I have given in Registration form is

Imports System.Data
Imports System.Data.Sql Client
Public Class Registration1
Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

End Sub
Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l
Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents TextBox2 As System.Web.UI.W ebControls.Text Box
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub

#End Region
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim str, str1 As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"

con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"
con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
str1 = "Insert into login values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
cmd = New SqlCommand(str1 , con)
cmd.ExecuteNonQ uery()
con.Close()
Response.Write( "Inserted")
End Sub
End Class

The coding I have given in loginform is


Imports System.Data
Imports System.Data.Sql Client

Public Class Login
Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.SqlConnectio n1 = New System.Data.Sql Client.SqlConne ction
'
'SqlConnection1
'
Me.SqlConnectio n1.ConnectionSt ring = "workstatio n id=""AURO-RA4"";packet size=4096;user id=sa;data source=""AURO-RA4"";pers" & _
"ist security info=False;init ial catalog=suganya "

End Sub
Protected WithEvents Panel1 As System.Web.UI.W ebControls.Pane l
Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label3 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label4 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on
Protected WithEvents LinkButton1 As System.Web.UI.W ebControls.Link Button
Protected WithEvents LinkButton2 As System.Web.UI.W ebControls.Link Button
Protected WithEvents SqlConnection1 As System.Data.Sql Client.SqlConne ction
Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box
Protected WithEvents TextBox2 As System.Web.UI.W ebControls.Text Box

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub

#End Region
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim rd As New SqlDataReader
Dim str As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"
con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

Dim cmd As New SqlCommand("sel ect count(*) from login where uname=' " & TextBox1.Text & " ' and pasword=' " & TextBox2.Text & " ' ", con)

rd = cmd.ExecuteRead er()

If rd(0) = 0 Then
Response.Write( "Invalid User")

Exit Sub

Else
Response.Write( "Valid User")

End If

End Sub

Private Sub LinkButton2_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles LinkButton2.Cli ck
Response.Redire ct("Registratio n1.aspx")


End Sub
End Class
Mar 10 '08 #1
2 3331
kunal pawar
297 Contributor
I guess Problem is in ur declaration statement.
record set object should not declare with new keyword
Dim rd As New SqlDataReader

it should be
Dim rd As SqlDataReader
Mar 10 '08 #2
saran23
28 New Member
Hi friends
This is sweatha, fresher MCA 2007 batch. Right now I am working in asp.net platform.
My dillema is that I have created a registration form with the fields as username & password. And if once the user register the username and password, it will be stored in the database(SQL Server 2000) by showing "Records Inserted".

Then I have created the login form just like the fields as username and password.And if the user enters the values it should check in the database & it should display whether the data is valid or invalid. Here, I have written the coding but the problem is with the datareader. If I run the page it is showing the error as "c:\inetpub\www root\extra1\Log in.aspx.vb(45): 'System.Data.Sq lClient.SqlData Reader.Private Sub New(command As System.Data.Sql Client.SqlComma nd)' is not accessible in this context because it is 'Private'." And also if I run the page instead of displaying valid or invalid user, It is displaying as "records inserted" which I have given in the registration page.

The Coding I have given in Registration form is

Imports System.Data
Imports System.Data.Sql Client
Public Class Registration1
Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()

End Sub
Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l
Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents TextBox2 As System.Web.UI.W ebControls.Text Box
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub

#End Region
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim str, str1 As String
Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"

con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"
con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
str1 = "Insert into login values('" & TextBox1.Text & "','" & TextBox2.Text & "')"
cmd = New SqlCommand(str1 , con)
cmd.ExecuteNonQ uery()
con.Close()
Response.Write( "Inserted")
End Sub
End Class

The coding I have given in loginform is


Imports System.Data
Imports System.Data.Sql Client

Public Class Login
Inherits System.Web.UI.P age

#Region " Web Form Designer Generated Code "

'This call is required by the Web Form Designer.
<System.Diagnos tics.DebuggerSt epThrough()> Private Sub InitializeCompo nent()
Me.SqlConnectio n1 = New System.Data.Sql Client.SqlConne ction
'
'SqlConnection1
'
Me.SqlConnectio n1.ConnectionSt ring = "workstatio n id=""AURO-RA4"";packet size=4096;user id=sa;data source=""AURO-RA4"";pers" & _
"ist security info=False;init ial catalog=suganya "

End Sub
Protected WithEvents Panel1 As System.Web.UI.W ebControls.Pane l
Protected WithEvents Label1 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label2 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label3 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Label4 As System.Web.UI.W ebControls.Labe l
Protected WithEvents Button1 As System.Web.UI.W ebControls.Butt on
Protected WithEvents LinkButton1 As System.Web.UI.W ebControls.Link Button
Protected WithEvents LinkButton2 As System.Web.UI.W ebControls.Link Button
Protected WithEvents SqlConnection1 As System.Data.Sql Client.SqlConne ction
Protected WithEvents TextBox1 As System.Web.UI.W ebControls.Text Box
Protected WithEvents TextBox2 As System.Web.UI.W ebControls.Text Box

'NOTE: The following placeholder declaration is required by the Web Form Designer.
'Do not delete or move it.
Private designerPlaceho lderDeclaration As System.Object

Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Init
'CODEGEN: This method call is required by the Web Form Designer
'Do not modify it using the code editor.
InitializeCompo nent()
End Sub

#End Region
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim rd As New SqlDataReader
Dim str As String

Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles MyBase.Load
'Put user code to initialize the page here
str = "user id=sa;password= ;data source=AURO-RA4;initial catalog=suganya ;server=AURO-RA4"
con = New SqlConnection(s tr)
Try
con.Open()
Catch
End Try
End Sub

Private Sub Button1_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button1.Click

Dim cmd As New SqlCommand("sel ect count(*) from login where uname=' " & TextBox1.Text & " ' and pasword=' " & TextBox2.Text & " ' ", con)

rd = cmd.ExecuteRead er()

If rd(0) = 0 Then
Response.Write( "Invalid User")

Exit Sub

Else
Response.Write( "Valid User")

End If

End Sub

Private Sub LinkButton2_Cli ck(ByVal sender As System.Object, ByVal e As System.EventArg s) Handles LinkButton2.Cli ck
Response.Redire ct("Registratio n1.aspx")


End Sub
End Class
Hi Swetha,
Going thru ur code, I found some mistakes,
I could not say the exact reason for the exception,but theres a lot to change in ur code,
1. Connection.Open () must be used carefully, it must be used just before executing the command, it must be

Expand|Select|Wrap|Line Numbers
  1. conn.open()
  2. cmd.executequery()
  3. conn.close()
The reason for this is in a real time the database server resides somewhere else, so the connection with the DB server must be minimum, opening the connection more than once and for a long time must be strictly avoided, as it creates network traffic and slow performance.
The connection string must be declared globally for a page and used it wherever needed,
there must be connection to the DB only when there is a need, In this u hv connected in PageLoad which does nothing,

2.Use the New keyword only when ur going to use the class,
for eg,
Expand|Select|Wrap|Line Numbers
  1. Dim cmd As New SqlCommand
must be like this,

Expand|Select|Wrap|Line Numbers
  1. Dim cmd As SqlCommand
-- this must be declared only once globally
and when ur using it anywhere
Expand|Select|Wrap|Line Numbers
  1. cmd=New Sqlcommand(ur command,conn)

3.When u want to just redirect to new page, without any code to be executed, just use the postbackurl property of the button, seperate event must not be used.

Im not much aware of VB, Im a C# guy, so im not sure whether there is Protected access modifier in Vb, If u hav better use it for Button Click events.
We do so. The datareader problem might be even due to access levels.

Though my info might not help to fix ur exact problem, it helps to know things in a professinal manner,as ur in Learning Phase.There is lot more to explore professionally, just search for some code in this forum and use it as a reference.

Thanks
Saravanan
Mar 11 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

3
373
by: sam | last post by:
Hello group, I have a function which is used to initiate sqlDataReader object. I was trying to invoke the close method on the DataReader object but cant really do that as the function returns a datareader and cannot access the datareader once the connection is closed. Here is what I do: public function getDataReader() as datareader
1
1397
by: VIJAY KUMAR | last post by:
Hi, Problem: Return ArrayList where Values are Added using DataReader. Note: My Data Reader returns M Columns and N Rows I am using DataReader in Data Access layer. I want to return this result to Business Layer. But, considering performace and Efficiency, I have to Close the DataReader Connection.
5
1958
by: Scott Shuster | last post by:
Hi, I'm having some strange problems in VS employing a DataReader. Here's a small snippet of the code: .... objDR = objCMD.ExecuteReader() If objDR.Read() Then (true stuff) Else
5
3027
by: DaM | last post by:
Hi guys, I'm having this problem with my ASP.Net application: I was testing the whole site, and it seem to work fine. Fast and stable, but suddenly it stopped working and this error occurred: "Timeout expired. The timeout period elapsed prior to obtaining a connection from the pool. This may have occurred because all pooled connections were in use and max pool size was reached."
1
3087
by: Brent | last post by:
I'm having a hard time wrapping my head around how to build a multi-dimensional array of n length out of a DataReader loop. Take this pseudo-code: ======================================= public string get_array(string sql) { //create db connection & open
11
2237
by: Siv | last post by:
Hi, I seem to be having a problem with a DataAdapter against an Access database. My app deletes 3 records runs a da.update(dt) where dt is a data.Datatable. I then proceed to update a list to reflect that the 3 items have been deleted only to discover that the 3 items appear, however when I click on them to display their information which runs a datareader over the same database it appears that the data has now gone. I wondered whether...
5
2364
by: Chad | last post by:
I want to create a class which contains a date, integer and string property which all allow a NULL value. I don't want to store the values in an Object datatype. I prefer more strongly typed data. I am interested in using SQL data types for my property types, but in our 4-tier app, we have a Data access layer that passes dataset across tiers to the Application layer, which populates the Business object's properties. In the Data layer, we...
5
2049
by: Varangian | last post by:
Hello there people, I'm having some kind of problem. I have a function that returns a datareader. At some point using the application I get an error "Unspecified error" (ssssoooo helpful) :). I think I know the problem. My Connection remains open. Is there a way I can do to close the Connection. below is the code. Thank you very much as always
3
9841
by: =?Utf-8?B?ZGVuIDIwMDU=?= | last post by:
Hi, Trouble in retaining values of dropdownlist, textboxes, and other controls when dropdownlist selectedindexchanged event is triggered, the controls are inside a user control and this user control inside a parent user control with an update panel. Can you guys help me hwo to retain the values. I have set EnableViewState to true. Where is correct page event to store entered and selected values before the values on controls are...
2
1673
by: =?Utf-8?B?TWlrZVo=?= | last post by:
I created a function to use DAAB return a datareader. The connection must keep opening when I consume the datareader. 1. I tested the function, it works. Even I use the datareader after 10 seconds. 2. DAAB document said that it manages the connection. 3. From .NET programming point view, no guaranty for the connection opens. Any suggestion? Thanks.
0
8437
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8861
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8778
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8549
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8636
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7375
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6187
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5660
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
1
2764
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.