Results 1 to 5 of 5

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
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    You could do a count before you try to retrieve the records:
    "SELECT Count(*) FROM tblUsers Where UserName = '" & TextBox1.Text & "'"

    You would use it something like this:

    Dim i as Integer
    i = sqlcmdValidate.ExecuteScalar

  3. #3
    Your Ad Here! Edneeis's Avatar
    Join Date
    Feb 2000
    Location
    Moreno Valley, CA (SoCal)
    Posts
    7,339
    Try looping through to get the results if any. Also if you are only trying to get the first row of results then you may want to use the SingleRow option for optimization.

    VB Code:
    1. Do While sqldtrValidate.Read
    2.  
    3.         TextBox1.Text = sqldtrValidate("UserName")
    4.         TextBox2.Text = sqldtrValidate("Password")
    5.         TextBox3.Text = sqldtrValidate("Role")
    6.  
    7.         Loop

  4. #4
    PowerPoster hellswraith's Avatar
    Join Date
    Jul 2002
    Location
    Washington St.
    Posts
    2,464
    I didn't even think about that, you could put the results in a DataRow object, then just check if it is null.

    If the UserName is a unique column, you will only get one result back anyway.

  5. #5

    Thread Starter
    ^:^...ANGEL...^:^ wrack's Avatar
    Join Date
    Mar 2002
    Location
    Melbourne, AUSTRALIA
    Posts
    2,695
    Thanks for ur help guyz...I did it this way...
    VB Code:
    1. If sqldtrValidate.Read() Then
    2.             TextBox1.Text = sqldtrValidate("UserName")
    3.             TextBox2.Text = sqldtrValidate("Password")
    4.             TextBox3.Text = sqldtrValidate("Role")
    5.         End If

    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