locked
code of vb 2005

    Question

  • As I work in vb 6. and dont know much about VB 2005. i need a connection code to connect my application with SQL 2005. i had used SQLConnection SQLAdapter SQLCommand to connect it had been connected but i want to reterive fields from table

    the code i write is

    Dim conn As SqlConnection

    conn = New SqlConnection("Data Source=.\SQLEXPRESS;Initial Catalog=Car_Golf_db;Integrated Security=True")

    conn.Open()

    ''Dim i As Integer

    ''Dim aa As String

    Dim rs As SqlCommand

    Dim strsql As String

    strsql = "select * from login where username='" & uname.Text & "' and password='" & password.Text & "'"

    rs = New SqlCommand(strsql, conn)

    rs.CommandType = CommandType.Text

    'rs.ExecuteReader(strsql)

    If rs.ExecuteReader.FieldCount > 0 Then

    Me.Hide()

    frmsel.Show()

    End If

    now i cant get it where it goes wrong. Please reply.

    gauri

     

     

    Friday, June 09, 2006 12:49 PM

All replies

  •  

    Hi and welcome to the MSDN fora.

    It will be help to all users of the community to read this:

    http://forums.microsoft.com/MSDN/ShowPost.aspx?PostID=464111&SiteID=1

    Thank you!

    Friday, June 09, 2006 1:26 PM
  • You're not saying what is going wrong and I don't see any wrong with your code.  If I'd hazard a guess, your "frmsel" pops up but doesn't show anything.  That's because it doesn't know anything about "rs".  Add a public method to frmsel that takes a SqlDataReader as an argument so that you can populate the controls in the form with the data.  Something like this:
      dim reader as SqlDataReader
      reader = rs.ExecuteReader
      if frmsel.DisplayData(reader) then me.hide()
    and in frmsel:
      public function DisplayData(rs as SqlDataReader) as boolean
        if not rs.hasrows then return false
        txtUser = rs.Item("User").ToString()
        txtPass = rs.Item("Password").ToString()
        me.show()
        return true
      end sub
    I didn't try it myself, please excuse any typos...

    Friday, June 09, 2006 2:01 PM
    Moderator
  • hi,

    your code is ok, but you just opened the connection without doing anything

    you didn't use any adapter in your code , nor datareader to read from your database

    how to featch data into your app  http://msdn2.microsoft.com/en-us/library/ms171918(VS.80).aspx

    hope this helps

    Friday, June 09, 2006 7:25 PM
  • hi,

    Is your problem solved?

    Thank you,
    Bhanu.

    Saturday, June 10, 2006 1:01 AM
  •  Gaurip wrote:

    'rs.ExecuteReader(strsql)
    If rs.ExecuteReader.FieldCount > 0 Then
    Me.Hide()
    frmsel.Show()
    End If

    What you need to take into consideration is the return value of ExecuteReader() method

    Dim dr as SqlDataReader = rs.ExecuteReader(strsql)

    This will give you the data reader that you can use Read() method on to make it point to the next row in the resultset. When it returns false there is no more rows.

    When you call Read() it will contain a row of  data and columns can be retrieved via the IDataRecord interface as it implements it on the SqlDataReader class
    http://msdn2.microsoft.com/en-us/system.data.idatarecord_members.aspx

    Saturday, June 10, 2006 9:28 PM