473,402 Members | 2,053 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,402 software developers and data experts.

Database Problems

ANGELSDAGGER
I have a client server application in VB.NET. I have a table named login_details, column names Password and Username. I have two variables that are sent from the server called UsName and PWord.

Im having trouble with the Syntax for the query.

Psuedocode:

Select Password from login_details where Username = UsName

Can anyone write the correct query for this in VB? Everything I've tried so far has returned an error or nothing at all.

I'd appreciate any help.

Thanks
Aug 8 '07 #1
11 1198
Frinavale
9,735 Expert Mod 8TB
Hi Angelsdagger,

Have you seen the .NET Article on How to use a database in your program?

It gives examples on how to retrieve information from your database.


Cheers!

-Frinny
Aug 8 '07 #2
Yeah I looked at that but it didn't help. Here's the code I have... can anyone help me?
Expand|Select|Wrap|Line Numbers
  1. Try
  2.             'Access DB File path
  3.             Dim File_Path As String
  4.             'Put database file in
  5.             ' Projects\DatabaseProg\DatabaseProg\bin\Debug
  6.             'or equivalent folder depending on your folder structure
  7.             File_Path = AppDomain.CurrentDomain.BaseDirectory & "serverDetails.mdb"
  8.  
  9.             'Connection String for New DB File
  10.             Dim Access_ConnStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;" & " Data Source= " & File_Path
  11.             'OleDb Connection for database
  12.             Dim Access_Conn As OleDb.OleDbConnection = New OleDb.OleDbConnection(Access_ConnStr)
  13.             'Access_Conn = New OleDb.OleDbConnection(Access_ConnStr)
  14.             Access_Conn.Open()
  15.  
  16.             'Database is now open and can be queried or updated
  17.             'Define SQL query and send to database
  18.             Dim strSQL As String
  19.             Dim cmd As OleDbCommand = Access_Conn.CreateCommand()
  20.             Dim cmd2 As OleDbCommand = Access_Conn.CreateCommand()
  21.             'Create SQL query relevant to your database/table structure
  22.  
  23.  
  24.             strSQL = "SELECT Passwords FROM(login_details)WHERE (Usernames = " & UsName & ")"        'returns username column"
  25.  
  26.  
  27.             cmd.CommandText = strSQL
  28.  
  29.  
  30.             If strSQL = PWord Then
  31.                 MsgBox("OK")
  32.                 lstbox5entries.Items.Add(PWord)
  33.             End If
  34.  
  35.             Dim reader As OleDbDataReader
  36.             reader = cmd.ExecuteReader
  37.             reader.Read()
  38.  
  39.  
  40.  
  41.  
  42.  
  43.             'The result of the query is now available in "reader"
  44.  
  45.             Dim col As Integer
  46.             Dim colname, value As String
  47.  
  48.  
  49.             For col = 0 To reader.FieldCount
  50.                 colname = reader.GetName(col) 'Gets Column Name
  51.                 value = reader.GetValue(col).ToString 'Gets value for a
  52.                 ' given column number
  53.                 lstbox5entries.Items.Add(value)
  54.             Next
  55.             'Now clean up all connections and release resources
  56.             reader.Close()
  57.             cmd.Dispose()
  58.             cmd = Nothing
  59.             Access_Conn.Close()
  60.             Access_Conn.Dispose()
  61.             Access_Conn = Nothing
  62.         Catch ex As Exception 'if it all goes pear-shaped
  63.             MsgBox(ex.Message)
  64.         End Try
Aug 8 '07 #3
Frinavale
9,735 Expert Mod 8TB
Try changing your strSql from:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Passwords FROM(login_details)WHERE (Usernames = " & UsName & ")"        'returns username column"
  2.  
To be
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Passwords FROM login_details WHERE Usernames = '" & UsName & "'"        'returns username column"
Aug 8 '07 #4
Ok that worked, it put the password into the box, but I got a message box saying "Index outwith bounds of the array".

Any ideas?
Aug 8 '07 #5
Frinavale
9,735 Expert Mod 8TB
Ok that worked, it put the password into the box, but I got a message box saying "Index outwith bounds of the array".

Any ideas?
It's probably
Expand|Select|Wrap|Line Numbers
  1. For col = 0 To reader.FieldCount
  2.                 colname = reader.GetName(col) 'Gets Column Name
  3.                 value = reader.GetValue(col).ToString 'Gets value for a
  4.                 ' given column number
  5.                 lstbox5entries.Items.Add(value)
  6. Next
  7.  
Try changing it to
Expand|Select|Wrap|Line Numbers
  1. For col = 0 To reader.FieldCount -1
  2.                 colname = reader.GetName(col) 'Gets Column Name
  3.                 value = reader.GetValue(col).ToString 'Gets value for a
  4.                 ' given column number
  5.                 lstbox5entries.Items.Add(value)
  6. Next
  7.  
Aug 8 '07 #6
Frinavale
9,735 Expert Mod 8TB
Try changing your strSql from:
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Passwords FROM(login_details)WHERE (Usernames = " & UsName & ")"        'returns username column"
  2.  
To be
Expand|Select|Wrap|Line Numbers
  1. strSQL = "SELECT Passwords FROM login_details WHERE Usernames = '" & UsName & "'"        'returns username column"

Just so you know ....it is not a good idea to insert your variables directly into the Sql query string like you are doing here.

Make sure that anything you are using in the query string has been sanitized...and it is strongly recommended that you add parameters to your Sql commands as outlined in the how to use a database in your program article.

This is to make sure that you are securely using your database.
(I recommend looking this stuff up on MSDN for a more in depth understanding of why this is good practice)
Aug 8 '07 #7
You're a star, thank you!!!

One last question. Do you know how to call a function in VB.NET. I have the following:
Expand|Select|Wrap|Line Numbers
  1.  Public Function GetFileContents(ByVal value As String, ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String
  2.  
  3.         Dim strContents As String
  4.         Dim objReader As StreamReader
  5.         Dim bAns As Boolean = False
  6.         Dim i As Integer
  7.  
  8.         Try
  9.  
  10.             objReader = New StreamReader("Last5.txt")
  11.             For i = 0 To i < 4
  12.                 strContents = objReader.ReadToEnd()
  13.                 objReader.Close()
  14.                 Return strContents
  15.                 socket.SendData(strContents)
  16.             Next
  17.  
  18.         Catch Ex As Exception
  19.             ErrInfo = Ex.Message
  20.         End Try
  21.  
  22.  
  23.     End Function
  24.  
  25.     Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean
  26.  
  27.  
  28.         Dim bAns As Boolean = False
  29.         Dim objReader As StreamWriter
  30.         Try
  31.  
  32.  
  33.             objReader = New StreamWriter("Last5.txt")
  34.             objReader.Write(strData)
  35.             objReader.Close()
  36.             bAns = True
  37.         Catch Ex As Exception
  38.             ErrInfo = Ex.Message
  39.  
  40.         End Try
  41.         Return bAns
  42.     End Function
  43.  
  44.  
And I want to call that Function after my SQL query has a result but it won't let me. Do you know how to do this?
Aug 8 '07 #8
Frinavale
9,735 Expert Mod 8TB
I'm not quite sure what you are asking.
Your GetFileContents Function is supposed to return a String value....if it fails within the Try/Catch block your function doesn't return. Maybe this is your problem? I'd move the Return strContents outside of the Try/Catch block so that it always returns a String (even if it's empty).

I'm not quite sure what you mean by "it won't let you" call a function after you've retrieved your data from the database.....

To call a function in .NET you just...call it...

For instance if you were calling your GetFileContents function you would do something like
Expand|Select|Wrap|Line Numbers
  1. Dim myFileContents As String = GetFileContents(valueStr, fullPathStr)
  2.  
This will call the function and store the function's output into the myFileContents String....(but if there was a problem then nothing's returned...you should fix this)

Could you be more clear about the problem?

Thanks

-Frinny

You're a star, thank you!!!

One last question. Do you know how to call a function in VB.NET. I have the following:
Expand|Select|Wrap|Line Numbers
  1.  Public Function GetFileContents(ByVal value As String, ByVal FullPath As String, Optional ByRef ErrInfo As String = "") As String
  2.  
  3.         Dim strContents As String
  4.         Dim objReader As StreamReader
  5.         Dim bAns As Boolean = False
  6.         Dim i As Integer
  7.  
  8.         Try
  9.  
  10.             objReader = New StreamReader("Last5.txt")
  11.             For i = 0 To i < 4
  12.                 strContents = objReader.ReadToEnd()
  13.                 objReader.Close()
  14.                 Return strContents
  15.                 socket.SendData(strContents)
  16.             Next
  17.  
  18.         Catch Ex As Exception
  19.             ErrInfo = Ex.Message
  20.         End Try
  21.  
  22.  
  23.     End Function
  24.  
  25.     Public Function SaveTextToFile(ByVal strData As String, ByVal FullPath As String, Optional ByVal ErrInfo As String = "") As Boolean
  26.  
  27.  
  28.         Dim bAns As Boolean = False
  29.         Dim objReader As StreamWriter
  30.         Try
  31.  
  32.  
  33.             objReader = New StreamWriter("Last5.txt")
  34.             objReader.Write(strData)
  35.             objReader.Close()
  36.             bAns = True
  37.         Catch Ex As Exception
  38.             ErrInfo = Ex.Message
  39.  
  40.         End Try
  41.         Return bAns
  42.     End Function
  43.  
  44.  
And I want to call that Function after my SQL query has a result but it won't let me. Do you know how to do this?
Aug 8 '07 #9
Plater
7,872 Expert 4TB
Even if it didn't fail the try/catch block, it will still return before sending the data on that socket.
Aug 8 '07 #10
rachid
1
Expand|Select|Wrap|Line Numbers
  1. dim cmd as new sqlclient.sqlcommand
  2. cmd.commandtext="select pw from table where usname='  "& texbox1.text &"  ' "
  3. cmd.connection=con
  4. con.open()
  5. dim dr as sqldatareader=cmd.execunonquery
  6. while dr.read
  7. label1.text=dr(0)
  8. end while
  9. dr.close()
  10. con.close
Aug 8 '07 #11
Frinavale
9,735 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. dim cmd as new sqlclient.sqlcommand
  2. cmd.commandtext="select pw from table where usname='  "& texbox1.text &"  ' "
  3. cmd.connection=con
  4. con.open()
  5. dim dr as sqldatareader=cmd.execunonquery
  6. while dr.read
  7. label1.text=dr(0)
  8. end while
  9. dr.close()
  10. con.close
Thanks for your input Rachid :)
Aug 8 '07 #12

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

Similar topics

3
by: Mudge | last post by:
Hi, My hosting provider only allows me to use 50 connections to my MySQL database that my Web site will use. I don't know what this 50 connections means exactly. Does this mean that only 50...
3
by: cooldv | last post by:
i am running a website on Windows 2000 server with ASP 3 webpages and Access 2000 database. (with a hosting company) traffic is slow at this time but expect to grow. lately i have been reading...
0
by: Alex | last post by:
Hi all, I've been running a db2 V8.1 databasle to store my radius server accounting info for a *long* time and have never had any problems with it. Last week we had a power outage in our...
7
by: Andante.in.Blue | last post by:
Hello everyone! I've been working with a problematic legacy database for a while. While I am still fairly new to Access, the more I work with the database, the more problems I've uncovered. ...
3
by: Ryan Muller | last post by:
I have a database that I designed for the production floor of my company. One of the problems that I have encountered is that 2 or more users will try to access the database to enter information...
2
by: Jeff | last post by:
Does anyone know of any potential problems running a 2000 database with 2003? Also, what about installing all other Office products as 2003 versions but leaving Access as 2002 running a 2000...
3
by: MW de Jager | last post by:
I'm having endless problems with gaining access to an Access Database that sits on a different server. My ASP.NET app cannot gain control. The errror message I get is: The Microsoft Jet...
2
by: rustyc | last post by:
Well, here's my first post in this forum (other than saying 'HI' over in the hi forum ;-) As I said over there: ... for a little side project at home, I'm writing a ham radio web site in...
15
by: Bexm | last post by:
Hello I have searched through this forum and it seems some people are having similar problems to me but none of the fixes are fixing mine..! :( I have a table in my database that has two xml...
4
by: raidvvan | last post by:
Hi there, We have been looking for some time now for a database system that can fit a large distributed computing project, but we haven't been able to find one. I was hoping that someone can...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...

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.