473,382 Members | 1,717 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,382 software developers and data experts.

help with .net2005, sqlserver 2000, listbox textbox and SELECT...FROM

Hi, I have a huge problem which drives crazy for a month... I am
working on a database program and I am using .net2005 and Sql server
2003. In a form I have a listbox and two texboxes and there's also in
my database a table with three sells: "name", "e-mail" and "address".
The listbox shows the "name" and I want when I choose a name, the two
textboxes to show the other two sells ("name","address") that goes for
the name i chose in the table. And guess what? I don't know how!!! I
know I have something like "SELECT...FROM...WHERE...LIKE..." but the
best think I ever got is "false" in the two textboxes... Please tell
how to do that and also in which listbox's event I have to do this?
PLEASE HELP ME !!!! IT'S ARGENT !!! Thanx a lot =)

Jul 31 '07 #1
10 1750
On 31 Lug, 13:14, SanMigue...@gmail.com wrote:
Hi, I have a huge problem which drives crazy for a month... I am
working on a database program and I am using .net2005 and Sql server
2003. In a form I have a listbox and two texboxes and there's also in
my database a table with three sells: "name", "e-mail" and "address".
The listbox shows the "name" and I want when I choose a name, the two
textboxes to show the other two sells ("name","address") that goes for
the name i chose in the table. And guess what? I don't know how!!! I
know I have something like "SELECT...FROM...WHERE...LIKE..." but the
best think I ever got is "false" in the two textboxes... Please tell
how to do that and also in which listbox's event I have to do this?
PLEASE HELP ME !!!! IT'S ARGENT !!! Thanx a lot =)
it may become Gold if you post some code and a better explanation :-)

T

Jul 31 '07 #2
look I am not very good at .net and sql (and I am trying something
difficult? very clever of me =D) but I will do my best.
this is the best thing I've managed to do and it's not working.

Private Sub Customers_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

CustomersTableAdapter1.Fill(_Dol_Ammad_E_Store_Dat aSet11.Customers)
End Sub

Private Sub ListBox1_DisplayMemberChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListBox1.DisplayMemberChanged

Using connection As New SqlClient.SqlConnection("Data
Source=DRJAMES;Initial Catalog=Dol-Ammad(E-Store);Integrated
Security=True;")
connection.Open()
Using command As New SqlClient.SqlCommand("SELECT [e-mail]
>From Customers WHERE Name = '" & ListBox1.SelectedItem & "'",
connection)
TextBox2.Text = command.ExecuteScalar.ToString
End Using
End Using
End Sub

Now, I don't know what else to sent you and to be honest I don't have
anything else done on this problem... If need something else tell me
what and I'll try to sent it. Thanx for your interest.

Jul 31 '07 #3
<Sa*********@gmail.comschrieb
Hi, I have a huge problem which drives crazy for a month... I am
working on a database program and I am using .net2005 and Sql server
2003. In a form I have a listbox and two texboxes and there's also
in my database a table with three sells: "name", "e-mail" and
"address". The listbox shows the "name" and I want when I choose a
name, the two textboxes to show the other two sells
("name","address") that goes for the name i chose in the table. And
guess what? I don't know how!!! I know I have something like
"SELECT...FROM...WHERE...LIKE..." but the best think I ever got is
"false" in the two textboxes... Please tell how to do that and also
in which listbox's event I have to do this? PLEASE HELP ME !!!! IT'S
ARGENT !!! Thanx a lot =)

Assuming that the record exists and the name is unique:

'untested:
Private Sub ListBox1_SelectedIndexChanged( _
ByVal sender As System.Object, ByVal e As System.EventArgs) _
Handles ListBox1.SelectedIndexChanged

Dim name As String
Dim cmd As SqlCommand
Dim dr As SqlDataReader

name = ListBox1.SelectedItem.ToString

cmd = New SqlCommand("select * from persons where name = @name")
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value = name

dr = cmd.ExecuteReader
dr.Read()
txtAddress.Text = dr("address").ToString
txtEMail.Text = dr("[e-mail]").ToString
dr.Close()

End Sub
Though, is the name unique? If yes, why do you use LIKE, and if no, what if
there are several records with the same name?
Armin

Jul 31 '07 #4
On 31 Lug, 13:54, SanMigue...@gmail.com wrote:
look I am not very good at .net and sql (and I am trying something
difficult? very clever of me =D) but I will do my best.
this is the best thing I've managed to do and it's not working.

Private Sub Customers_Load(ByVal sender As System.Object, ByVal e As
System.EventArgs) Handles MyBase.Load

CustomersTableAdapter1.Fill(_Dol_Ammad_E_Store_Dat aSet11.Customers)
End Sub

Private Sub ListBox1_DisplayMemberChanged(ByVal sender As Object,
ByVal e As System.EventArgs) Handles ListBox1.DisplayMemberChanged

Using connection As New SqlClient.SqlConnection("Data
Source=DRJAMES;Initial Catalog=Dol-Ammad(E-Store);Integrated
Security=True;")
connection.Open()
Using command As New SqlClient.SqlCommand("SELECT [e-mail]>From Customers WHERE Name = '" & ListBox1.SelectedItem & "'",

connection)
TextBox2.Text = command.ExecuteScalar.ToString
End Using
End Using
End Sub

Now, I don't know what else to sent you and to be honest I don't have
anything else done on this problem... If need something else tell me
what and I'll try to sent it. Thanx for your interest.
try ExecuteReader and get the field values you need.

ExecuteScalar returns 1 value and there is no way you can fill 2
TexBoxes with 1 value.
Further, that value may not be what you want, as for instance is a
field of no interest to you.

T

Jul 31 '07 #5
Yes the name is unique and I was using LIKE in this way:

SqlDataAdapter1.SelectCommand.CommandText = "SELECT e-mail FROM
persons WHERE name LIKE '" & ListBox1.Text & "'"
TextBox2.Text = SqlDataAdapter1.ToString

I tried what you sent me but still nothing happend. =( there's no
problem with the code, the program runs without faults and when I
change the listbox item the two textboxes remain empty. WHY???????? I
know that the solution is like the one you sent but it doesn't work.

Jul 31 '07 #6
"Armin Zingler" <az*******@freenet.deschrieb
>
cmd = New SqlCommand("select * from persons where name = @name")
Sry, forgot the connection:

cmd = New SqlCommand("select * from persons where name = @name", con)
And too many brackets:
txtEMail.Text = dr("[e-mail]").ToString
txtEMail.Text = dr("e-mail").ToString
Armin
Jul 31 '07 #7
<Sa*********@gmail.comschrieb
Yes the name is unique and I was using LIKE in this way:

SqlDataAdapter1.SelectCommand.CommandText = "SELECT e-mail FROM
persons WHERE name LIKE '" & ListBox1.Text & "'"
TextBox2.Text = SqlDataAdapter1.ToString

I'm a bit confused because, in the other posting

a) you don't use LIKE
b) the last line is

TextBox2.Text = command.ExecuteScalar.ToString

c) you use Listbox1.SelectedItem, not Listbox1.Text

The latter shouldn't work if you enable Option Strict.

Armin
Jul 31 '07 #8
Oh, sorry. My fault! Let's forget completely the LIKE thing and all
the stupid code I wrote... ( I told you that obviously I was writting
whatever I found because it's almost a month I am trying to fix this,
so everything is wrong!!!).
Now, the code I am using right now - and still not working... - is
this:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Dim name As String
Dim cmd As SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader

name = ListBox1.SelectedItem.ToString

cmd = New SqlClient.SqlCommand("SELECT * FROM Customers WHERE
name = @name", SqlConnection1)
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value =
name

dr = cmd.ExecuteReader
dr.Read()
TextBox3.Text = dr("address").ToString
TextBox2.Text = dr("E-mail").ToString
dr.Close()

End Sub

Do you find anything wrong???

Jul 31 '07 #9
<Sa*********@gmail.comschrieb
Oh, sorry. My fault! Let's forget completely the LIKE thing and all
the stupid code I wrote... ( I told you that obviously I was
writting whatever I found because it's almost a month I am trying to
fix this, so everything is wrong!!!).
Now, the code I am using right now - and still not working... - is
this:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Dim name As String
Dim cmd As SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader

name = ListBox1.SelectedItem.ToString

cmd = New SqlClient.SqlCommand("SELECT * FROM Customers WHERE
name = @name", SqlConnection1)
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value =
name

dr = cmd.ExecuteReader
dr.Read()
TextBox3.Text = dr("address").ToString
TextBox2.Text = dr("E-mail").ToString
dr.Close()

End Sub

Do you find anything wrong???
No, nothing wrong.

Are you sure you connected to the right database? Right table? Really no
duplicate names?
I guess you have checked this alredy, but...
Is it possible that you can send me the project via e-mail?
Use the sender address of this post and insert a "_" between "no" and
"spam".
Armin

Jul 31 '07 #10
On Jul 31, 8:50 am, SanMigue...@gmail.com wrote:
Oh, sorry. My fault! Let's forget completely the LIKE thing and all
the stupid code I wrote... ( I told you that obviously I was writting
whatever I found because it's almost a month I am trying to fix this,
so everything is wrong!!!).
Now, the code I am using right now - and still not working... - is
this:

Private Sub ListBox1_SelectedIndexChanged(ByVal sender As
System.Object, ByVal e As System.EventArgs)
Dim name As String
Dim cmd As SqlClient.SqlCommand
Dim dr As SqlClient.SqlDataReader

name = ListBox1.SelectedItem.ToString

cmd = New SqlClient.SqlCommand("SELECT * FROM Customers WHERE
name = @name", SqlConnection1)
cmd.Parameters.Add("@name", SqlDbType.NVarChar, 50).Value =
name

dr = cmd.ExecuteReader
dr.Read()
TextBox3.Text = dr("address").ToString
TextBox2.Text = dr("E-mail").ToString
dr.Close()

End Sub

Do you find anything wrong???
Try running your code with the SQL Profiler running a trace. Verify
that the SQL statement being executed is what you expect it to be. You
are still using the .SelectedItem property as opposed to the.Text
property, that may be part of the problem. Perhaps single step through
the code and after the variable "name" is assigned a value, dump the
value in the command window and verify the variable was assigned the
correct value.
Jul 31 '07 #11

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Raj | last post by:
Hi all, Can anyone help me with a script which would delete files or move them to a different folder at some scheduled time..! Please.....!!! Thanks in advance...
19
by: Thue Tuxen Sørensen | last post by:
Hi everybody ! I´m maintaining a large intranet (approx 10000 concurrent users) running on one IIS box and one DB box with sqlserver 2000. Currently there is 2,5 GB Ram, 1 1400 mhz cpu and 2...
9
by: Megan | last post by:
Hi- I'm creating a database of music bands with their cds and songs. I'm trying to program an SQL statement so that I can enter a string of text in a textbox, press the 'Enter' key, and have...
5
by: Melissa Cowan | last post by:
I am using Access 2000. I have the Developer's handbook and got the code for the mulit select listbox from there. It sends the selected value to another listbox, lstselected. What I need to do is...
1
by: Edward | last post by:
I am having a terrible time getting anything useful out of a listbox on my web form. I am populating it with the results from Postcode lookup software, and it is showing the results fine. What...
0
by: BigAl.NZ | last post by:
Hi Guys, I am trying to write/copy some code that uses events with a GPS. Everytime the GPS position updates the event fires. The GPS code is from a SDK Library that I got called GPS Tools...
1
by: colleen1980 | last post by:
Hi: I am trying to pull all the values from the listbox. But the ASP code shows only the last record. Needs help HTML <html> <head>
9
by: zdrakec | last post by:
Hello all: Clearly, I'm not getting it! Here is the scenario: On a web page, I have two list boxen and a text box. The first listbox is populated at page load time (if it is not a postback)....
2
by: teddymeu | last post by:
Hi Guys, this is kinda complicated but ill do my best to explain. I have two tables. products and categories. Products holds product info and an image, its primary key is ProductID. Category table...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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...

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.