Results 1 to 19 of 19

Thread: ACCESSING Data from SQL Server...

  1. #1

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695

    Thumbs down ACCESSING Data from SQL Server...

    Hi I am trying to access Data from SQL Server database and so far I am successful only if the record exists...The code I using is as follows...
    VB Code:
    1. Imports System.Data.SqlClient
    2.  
    3. Public Class Main
    4.     Inherits System.Web.UI.Page
    5.     Protected WithEvents TextBox1 As System.Web.UI.WebControls.TextBox
    6.     Protected WithEvents TextBox2 As System.Web.UI.WebControls.TextBox
    7.     Protected WithEvents TextBox3 As System.Web.UI.WebControls.TextBox
    8.     Protected WithEvents Button1 As System.Web.UI.WebControls.Button
    9.  
    10. #Region " Web Form Designer Generated Code "
    11.  
    12.     'This call is required by the Web Form Designer.
    13.     <System.Diagnostics.DebuggerStepThrough()> Private Sub InitializeComponent()
    14.  
    15.     End Sub
    16.  
    17.     Private Sub Page_Init(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Init
    18.         'CODEGEN: This method call is required by the Web Form Designer
    19.         'Do not modify it using the code editor.
    20.         InitializeComponent()
    21.     End Sub
    22.  
    23. #End Region
    24.  
    25.     Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    26.         'Put user code to initialize the page here
    27.     End Sub
    28.  
    29.     Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
    30.         Dim strUserName As String
    31.         Dim strPassword As String
    32.  
    33.         Dim sqlcnnValidate As SqlConnection
    34.         Dim sqlcmdValidate As SqlCommand
    35.         Dim sqldtrValidate As SqlDataReader
    36.         Dim strSQL As String
    37.  
    38.         sqlcnnValidate = New SqlConnection("server=WRACK\SQLDB;uid=sa;pwd=;database=NW;")
    39.  
    40.         strSQL = "SELECT [UserName], [Password], [Role] FROM tblUsers Where UserName = '" & TextBox1.Text & "' ORDER BY [UserName]"
    41.  
    42.         sqlcmdValidate = New SqlCommand(strSQL, sqlcnnValidate)
    43.  
    44.         sqlcnnValidate.Open()
    45.  
    46.         sqldtrValidate = sqlcmdValidate.ExecuteReader
    47.  
    48.         sqldtrValidate.Read()
    49.  
    50.         TextBox1.Text = sqldtrValidate("UserName")
    51.         TextBox2.Text = sqldtrValidate("Password")
    52.         TextBox3.Text = sqldtrValidate("Role")
    53.  
    54.         sqldtrValidate.Close()
    55.         sqlcnnValidate.Close()
    56.     End Sub
    57. End Class
    If the username I put in Textbox1 doesn't exists in database then I am getting this error (please see the picture attached). Is there anyway to make sure that the record exists...like RecordSet.RecordCount (in VB 6)...

    I am using ASP.NET with VB.NET (ASP.NET Web Application from VB.NET)...

    Also if someone can show me how to add data to database from textboxes then I would really appreciate it...

    Cheers...
    Attached Images Attached Images  

  2. #2
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    You shoud use 'Read' properly, before accessing data:
    VB Code:
    1. While  sqldtrValidate.Read()
    2.  
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.  
    7. End While

    Edits: Or you may use:
    VB Code:
    1. If sqldtrValidate.Read()
    2.  
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.  
    7. End If
    Last edited by Lunatic3; May 30th, 2003 at 06:24 AM.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  3. #3

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Lunatic3
    You shoud use 'Read' properly, before accessing data:
    VB Code:
    1. While  sqldtrValidate.Read()
    2.  
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.  
    7. End While

    Edits: Or you may use:
    VB Code:
    1. If sqldtrValidate.Read()
    2.  
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.  
    7. End If
    Thanks M8...works nice...so basically .Read() works something like RecordCount being 1 or more...??? Correct me if I am wrong...

    Cheers...

  4. #4
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Not exactly what you said:
    sqldatareader.Read does two things:
    -Advances to the next record
    -Returns true if there are more rows; otherwise, false.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Lunatic3
    Not exactly what you said:
    sqldatareader.Read does two things:
    -Advances to the next record
    -Returns true if there are more rows; otherwise, false.
    what if I want to go back to previous record...???

    Cheers...

  6. #6
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I am afraid you can not do that with datareader as datareader is Forward Only
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  7. #7

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by Lunatic3
    I am afraid you can not do that with datareader as datareader is Forward Only
    Can u please give me an example of another object through which I can do all the stuff I used to do with VB 6 and ADODB.Recordset...

    If can't then atleast something which allows me to go back to previous record...last record...first record...

    Cheers...

  8. #8
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Use a DataSet.

  9. #9

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by DevGrp
    Use a DataSet.
    To be honest I am completly new to .NET and I will highly appreciate if you post me a small example on how to use the dataset with connection object...!!!

    Cheers...

  10. #10
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464

  11. #11

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by hellswraith
    Can you search?
    http://www.google.com/search?hl=en&l...aset+in+vb.net
    sure M8...I will try...just looking for shortcut as I have not much time and exams are coming closer...

    Cheers...

  12. #12
    Frenzied Member DevGrp's Avatar
    Join Date
    Nov 2001
    Location
    Charlotte, NC
    Posts
    1,256
    Originally posted by wrack
    Thanks M8...works nice...so basically .Read() works something like RecordCount being 1 or more...??? Correct me if I am wrong...

    Cheers...
    Also if you are using 1.1 version of the framework, the DataReader has a HasRows property, so you can do this

    VB Code:
    1. If (sqldtrValidate.HasRows = True) Then
    2.    While sqldtrValidate.Read()
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.    End While
    7. End If

  13. #13

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by DevGrp
    Also if you are using 1.1 version of the framework, the DataReader has a HasRows property, so you can do this

    VB Code:
    1. If (sqldtrValidate.HasRows = True) Then
    2.    While sqldtrValidate.Read()
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.    End While
    7. End If
    I have version 1 but I will install 1.1 and try that one...Just wondering that which method is much better...

    Cheers...

  14. #14
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82
    Just to say you cannot use Framework 1.1 within Visual Studio 2002. If you Want to use Framework 1.1, you have to run visual studio 2003.

  15. #15
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    Just to say you cannot use Framework 1.1 within Visual Studio 2002. If you Want to use Framework 1.1, you have to run visual studio 2003.
    This is not true, you can use 1.1 with 2002.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  16. #16

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Originally posted by matt3011
    Just to say you cannot use Framework 1.1 within Visual Studio 2002. If you Want to use Framework 1.1, you have to run visual studio 2003.
    I think you are wrong here bcos my friends are running 2002 with framework 1.1 installed...

    Cheers...

  17. #17
    Frenzied Member
    Join Date
    Oct 2002
    Location
    Gammapolis
    Posts
    1,474
    I think you are wrong here bcos my friends are running 2002 with framework 1.1 installed...
    He is not wrong because of what you are saying. Normally VS.NET 2002 compiles against Framework 1.0 even if u have framework 1.1 installed. However you can force your code to to compile against 1.1. Still your ASP.NET application will run under 1.1 even if you dont force it.
    'Heading for the automatic overload'
    Marillion, Brave, The Great Escape, 1994

    'How will WE stand the FIRE TOMORROW?'
    Eloy, Silent Cries and Mighty Echoes, The Vision - Burning, 1979

  18. #18
    Lively Member matt3011's Avatar
    Join Date
    May 2002
    Location
    France
    Posts
    82
    Thanks Lunatic3, that is exactly what i wanted to say. Maybe I wasn't clear enough

  19. #19

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Thanks M8...Now I understand the difference and I think I should move to .NET 2003...

    Cheers...

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width