Results 1 to 19 of 19

Thread: ACCESSING Data from SQL Server...

Threaded View

  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  

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