|
-
Jul 15th, 2008, 11:55 AM
#1
Thread Starter
Hyperactive Member
[RESOLVED] [2008] check to see if select statement finds a result
i am kinda new to sql programing. just saying that to get it out of the way. i know how to connect to a database but i don't know how to check to see if a result is received from a select statement. this is my select statement
Code:
da.SelectCommand = New OleDbCommand("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text)
i need to know if there is a user with that username and if there is then i will need to read the password for that person. then if both username and password are correct then i will show form 2. any help is welcome.
-
Jul 15th, 2008, 12:03 PM
#2
Re: [2008] check to see if select statement finds a result
you can use
Code:
Dim dtUser As New DataTable("User")
Dim oldAdap As New OleDataAdapter("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text), objConn) 'objConn is connection object
oleAdap.Fill(dtUser )
If dtUser.rows.count = 1 then
'here is you code that will check password and other thing.
else
messagebox.show("Invalid user name")
End if
PS: Better use parameterised query.
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 12:04 PM
#3
Re: [2008] check to see if select statement finds a result
Fill the DataTable, and then take a count of rows.
Dim dt As New DataTable
da.Fill(dt)
dt.Rows.Count
-
Jul 15th, 2008, 12:17 PM
#4
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
trying code will get back to you soon
-
Jul 15th, 2008, 12:22 PM
#5
Re: [2008] check to see if select statement finds a result
Using a dataadapter is overkill, using a command's executescalar method should work faster.
-
Jul 15th, 2008, 12:46 PM
#6
Fanatic Member
Re: [2008] check to see if select statement finds a result
 Originally Posted by wild_bill
Using a dataadapter is overkill, using a command's executescalar method should work faster.
I would have to agree with wild__bill
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 15th, 2008, 01:14 PM
#7
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
how would i do that? i would not know where to start.
-
Jul 15th, 2008, 01:20 PM
#8
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
 Originally Posted by riteshjain1982
you can use
Code:
Dim dtUser As New DataTable("User")
Dim oldAdap As New OleDataAdapter("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text), objConn) 'objConn is connection object
oleAdap.Fill(dtUser )
If dtUser.rows.count = 1 then
'here is you code that will check password and other thing.
else
messagebox.show("Invalid user name")
End if
PS: Better use parameterised query.
if i used your code it would only work if there is one entry ( i could change that to a for loop but not atm) i would also need an way to check for the a password and the way you have it set up i would have to have a copy of a lookup table on the client side. which would have to be updated every time there was a new member. so your way is not very useful to me bu thank you for reponding so quickly
-
Jul 15th, 2008, 01:27 PM
#9
Fanatic Member
Re: [2008] check to see if select statement finds a result
 Originally Posted by bagstoper
Code:
da.SelectCommand = New OleDbCommand("SELECT * FROM userinfo WHERE username =" & UsernameTextBox.Text)
i need to know if there is a user with that username and if there is then i will need to read the password for that person. then if both username and password are correct then i will show form 2. any help is welcome.
If you do the following you should only return one record.
Code:
SELECT Password FROM userinfo WHERE username =" & UsernameTextBox.Text
Create a command object and use the executescalar method
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 15th, 2008, 01:28 PM
#10
Fanatic Member
Re: [2008] check to see if select statement finds a result
Note that if the user does not exist you not return a record but if the user exists then you will return the password
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 15th, 2008, 01:36 PM
#11
Fanatic Member
Re: [2008] check to see if select statement finds a result
If you have trouble with the command object there is a sample from today on our forum. Post #9 will be of interest to you.
http://www.vbforums.com/showthread.p...command+object
CLanguage; 
IF Post = HelpFull Then
RateMe
Else
Say("Shut UP")
End If
DotNet rocks
VB 6, VB.Net 2003, 2005, 2008, 2010, SQL 2005, WM 5.0,ahem ?OpenRoad?
-
Jul 15th, 2008, 03:15 PM
#12
Re: [2008] check to see if select statement finds a result
 Originally Posted by bagstoper
if i used your code it would only work if there is one entry ( i could change that to a for loop but not atm)
Is your table stores Multiple user for same username
 Originally Posted by bagstoper
i would also need an way to check for the a password
you can use dtUser.rows(0).Item("Password")
 Originally Posted by bagstoper
the way you have it set up i would have to have a copy of a lookup table on the client side. which would have to be updated every time there was a new member. so your way is not very useful to me bu thank you for reponding so quickly
can you explain Why/what is the need to store/Refresh table at client side?
and as suggested by other member,in case you are only intersted in getting Password for a user you can simply for for ExecuteReader and check if Return value is nothing (that mean user does not exist) or have Valid value.
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 04:00 PM
#13
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
i think i have firgured it out but now my connection string isnt working. this is my connection string
Code:
Provider=MySQLProv;Data Source=mydb;User Id=myUsername;Password=myPassword;
i can fill in the data source and user id and password but i dont know how to find my provider. any help?
-
Jul 15th, 2008, 04:09 PM
#14
Re: [2008] check to see if select statement finds a result
if it's SQL database simply use SQLClient objects instead of OLEDB,also look at Thisit has plenty of connection string sample for various database.
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 04:11 PM
#15
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
that is where i got the connection string from and this is a mySQL db will that matter
-
Jul 15th, 2008, 04:19 PM
#16
Re: [2008] check to see if select statement finds a result
can you post you code that fills/check the username (along with connection string and objects).
Also check if are you able to call open() method of oleConnection object?if not post the error message.
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 06:19 PM
#17
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
ok i have gotten everything working except with my connection string.
Code:
Driver={MySQL ODBC 3.51 Driver};Server=http://dansoft.selfip.com;Option=131072;Stmt=;Database=elemental_battle; User=****;Password=****;
my error
Code:
ERROR [HY000] [MySQL][ODBC 3.51 Driver]Unknown MySQL server host 'http://dansoft.selfip.com' (11004) ERROR [HY000] [MySQL][ODBC 3.51 Driver]Unknown MySQL server host 'http://dansoft.selfip.com' (11004)
when i change the connection string to where server=localhost then there is no error and i can get my data. i don't see the problem.
-
Jul 15th, 2008, 06:31 PM
#18
Re: [2008] check to see if select statement finds a result
never worked with MySQL,but you can cross this these point..
1>Are you able to connect to server 'http://dansoft.selfip.com' using some external tools? (e.g SQL Analyser in case of SQL 2000)
2>Make sure MySQL is running on same default port else explicitly specify it in your connection string.
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 06:35 PM
#19
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
can you recommend a good one?
-
Jul 15th, 2008, 06:42 PM
#20
Re: [2008] check to see if select statement finds a result
 Originally Posted by bagstoper
can you recommend a good one?
What? if it's tool may be you would like to Check this
__________________
Rate the posts that helped you 
-
Jul 15th, 2008, 06:45 PM
#21
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
that only has an external download link to this
-
Jul 15th, 2008, 07:46 PM
#22
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
well i found one but it also wont connect to the url but it will connect to the localhost. i get this error when it tries to connect to the url version (via visual studio)
Code:
ERROR [HY000] [MySQL][ODBC 3.51 Driver]Host '192.168.1.1' is not allowed to connect to this MySQL server ERROR [HY000] [MySQL][ODBC 3.51 Driver]Host '192.168.1.1' is not allowed to connect to this MySQL server
-
Jul 16th, 2008, 10:57 AM
#23
Thread Starter
Hyperactive Member
Re: [2008] check to see if select statement finds a result
solved it i had to create a new user in mySQL with the host being anyone and give them privileges to view the database.
-
Jul 16th, 2008, 11:53 AM
#24
Re: [RESOLVED] [2008] check to see if select statement finds a result
As a side note, if you're using MySQL, I'd recommend downloading the MySQL .NET provider off their site. It's a LOT faster and nicer to use than ODBC.
-
Jul 16th, 2008, 12:35 PM
#25
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2008] check to see if select statement finds a result
ok i will look into that i was wondering if there was any way i could make it go faster. it would take about 10 secs to connect and read the data for 2 rows. would hate to see how long it took to read 100+ rows. thanks i will into it.
-
Jul 16th, 2008, 12:49 PM
#26
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2008] check to see if select statement finds a result
wait do you mean the MySQL :: MySQL Connector/Net or something else?
-
Jul 16th, 2008, 04:21 PM
#27
Re: [RESOLVED] [2008] check to see if select statement finds a result
No, that's what I mean. The .NET connector (here). I believe the latest version is 5.2.2 and it works with VS2008. It appears like you're using the old ODBC driver v3.51 and talking to your database via the ADO.NET ODBC or OLEDB objects.
The .NET connector gives you a MySQL namespace with MySqlConnection, MySqlCommand, MySqlDataAdapter, etc objects to use. It's also a fair amount faster.
Last edited by Jenner; Jul 16th, 2008 at 04:24 PM.
-
Jul 16th, 2008, 04:24 PM
#28
Thread Starter
Hyperactive Member
Re: [RESOLVED] [2008] check to see if select statement finds a result
ok i just downloaded it how do i implement it into my project?
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|