•
•
•
•
What is DaniWeb IT Discussion Community?
You're currently browsing the C# section within the Software Development category of DaniWeb, a massive community of 402,098 software developers, web developers, Internet marketers, and tech gurus who are all enthusiastic about making contacts, networking, and learning from each other. In fact, there are 2,512 IT professionals currently interacting right now! Registration is free, only takes a minute and lets you enjoy all of the interactive features of the site.
Please support our C# advertiser: Programming Forums
Views: 6577 | Replies: 23 | Solved
![]() |
•
•
Join Date: Mar 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
Hi all,
In my C# application I have a SQL database with a table called Customers. In this table I have several columns like Name, Street, ZipCode and City.
I already managed to get data in and out the database with the use of datasets, tableadaptors and bindingsources and get them into textboxes and listboxes. So far no problem.
But…. the thing I want is to run a SQL query like below and get all the results in a single textbox or listbox. The important things is, that every entry in the textbox or listbox should be on a new line. Multi line for example.
So the result in the textbox should look like:
Ramon
Streetname
NewYork
1111AA
This is the statement I want to run where @1 is a variable.
SELECT Name, Street, ZipCode, City
FROM Customers
WHERE (CustomerID = @1)
I hope someone can point me to the right direction.
Cheers,
Ramon.
In my C# application I have a SQL database with a table called Customers. In this table I have several columns like Name, Street, ZipCode and City.
I already managed to get data in and out the database with the use of datasets, tableadaptors and bindingsources and get them into textboxes and listboxes. So far no problem.
But…. the thing I want is to run a SQL query like below and get all the results in a single textbox or listbox. The important things is, that every entry in the textbox or listbox should be on a new line. Multi line for example.
So the result in the textbox should look like:
Ramon
Streetname
NewYork
1111AA
This is the statement I want to run where @1 is a variable.
SELECT Name, Street, ZipCode, City
FROM Customers
WHERE (CustomerID = @1)
I hope someone can point me to the right direction.
Cheers,
Ramon.
In your dataset you can add some queries or paramatized queries and pass the value of its variable(s) at the runtime, on your DataTable right click then Add query and go through it....
Concerns Textbox you should do some process on the data return from database before showing them on the textbox, first to have multiple lines on textbox, set the from its properties or use RichTextBox..
Concerns Textbox you should do some process on the data return from database before showing them on the textbox, first to have multiple lines on textbox, set the from its properties or use RichTextBox..
B.Sc Computer Science, Helwan University
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
Microsoft Student Partner
Personal blog http://ramymahrous.blogspot.com/
Arabic technical blog http://fci-h-ar.blogspot.com/
English technical blog http://fci-h.blogspot.com/
•
•
Join Date: Mar 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
Hi RamyMahrous,
Thanks for your fast reply!
I already found out how to add the queries and their variables to the DataSet and run them. When running the queries I need a textbox for each collumn to display it's corresponding value.
But I need it to display in a single box, this can also be a Richtextbox, but I have no idea how.
Cheers,
Ramon.
Thanks for your fast reply!
I already found out how to add the queries and their variables to the DataSet and run them. When running the queries I need a textbox for each collumn to display it's corresponding value.
But I need it to display in a single box, this can also be a Richtextbox, but I have no idea how.
Cheers,
Ramon.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
SELECT Name + '\n' + Street + '\n' + ZipCode +'\n' + City FROM Customers WHERE (CustomerID = @1)
This assumes none of the columns allow NULLS. Otherwise you will need to use ISNULL(name, '') + '\n' + ... etc
Set the TextMode property of you textbox to Multiline (it will render to the browser as a textarea) set the rows property of the textbox to 4.
•
•
Join Date: Mar 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
Hi hollystyles,
Thanks for your query help, I got the statement below working and it gives me the desired result in in querybuilder.
SELECT ISNULL(Naam, Naam) + '\n' + ISNULL(Aanhef, Aanhef) + '\n' + ISNULL(Straat, Straat) + '\n' + ISNULL(Postcode, Postcode) + '\n' + ISNULL(Plaats, Plaats) AS Expr1
FROM Klanten
WHERE (KlantID = @1)
I run it using:
this.klantenTableAdapter.FillByKlantAdres(this.dorpshuisDataSet.Klanten, KlantID);
But I have no clue on how to get the result in a textbox, listbox or label ect.... I can only bind the textboxes to a single collumn from the bindingsource.
Can someone help me to get the result of the query in a textbox?
Ramon
Thanks for your query help, I got the statement below working and it gives me the desired result in in querybuilder.
SELECT ISNULL(Naam, Naam) + '\n' + ISNULL(Aanhef, Aanhef) + '\n' + ISNULL(Straat, Straat) + '\n' + ISNULL(Postcode, Postcode) + '\n' + ISNULL(Plaats, Plaats) AS Expr1
FROM Klanten
WHERE (KlantID = @1)
I run it using:
this.klantenTableAdapter.FillByKlantAdres(this.dorpshuisDataSet.Klanten, KlantID);
But I have no clue on how to get the result in a textbox, listbox or label ect.... I can only bind the textboxes to a single collumn from the bindingsource.
Can someone help me to get the result of the query in a textbox?
Ramon
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
The query has the single column you want 'Expr1' bind the textbox to that.
I am not very knowledgeable with BindingSources I am afraid, I don't like them. I prefer to have simple business objects.
So I would not do the concatenation in the TSQL (it doesn't really belong there, it belongs in the presentation layer or as a business rule) So I would have an Address class and an AddressSource class, the AddressSource is in the DataLayer and uses ADO to connect to the DB and lift the data and returns an address object, then make the Address object provide the concatenated address with carriage returns.
I attach an example project I knocked up in response to this thread. There is a .sql script in the AppGloablResources folder that creates the table and populates a demo record.
I am not very knowledgeable with BindingSources I am afraid, I don't like them. I prefer to have simple business objects.
So I would not do the concatenation in the TSQL (it doesn't really belong there, it belongs in the presentation layer or as a business rule) So I would have an Address class and an AddressSource class, the AddressSource is in the DataLayer and uses ADO to connect to the DB and lift the data and returns an address object, then make the Address object provide the concatenated address with carriage returns.
I attach an example project I knocked up in response to this thread. There is a .sql script in the AppGloablResources folder that creates the table and populates a demo record.
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
Oops perhaps you are doinf a Forms project not a web project.
I have started a forms project and I can't get the binding source to use the new query either yet. I am too tired at the moment.
One solution could be a computed column in the sql table, but that smells a bit to me.
I have started a forms project and I can't get the binding source to use the new query either yet. I am too tired at the moment.
One solution could be a computed column in the sql table, but that smells a bit to me.
•
•
Join Date: Mar 2008
Posts: 14
Reputation:
Rep Power: 1
Solved Threads: 0
Yes indeed I am making a Form app.
At least you point me a little closer to the solution, the query result is indeed stored in the Expr1. So I created a collumn Expr1 in my klantenDataTable where the query is also stored.
When I do a preview data from there I can run the query and see it's results in the Expr1 collumn. Now I can also bind the textbox to this collumn trough the bindingsource, but when I run the program and then the text shows only a blank line.
This is my first real program I am creating so sorry for my ignorance. But I am having fun with it.
You said you don't like bindingsources but instead you use simple business objects. What does that mean?
Ramon
At least you point me a little closer to the solution, the query result is indeed stored in the Expr1. So I created a collumn Expr1 in my klantenDataTable where the query is also stored.
When I do a preview data from there I can run the query and see it's results in the Expr1 collumn. Now I can also bind the textbox to this collumn trough the bindingsource, but when I run the program and then the text shows only a blank line.
This is my first real program I am creating so sorry for my ignorance. But I am having fun with it.
You said you don't like bindingsources but instead you use simple business objects. What does that mean?
Ramon
•
•
Join Date: Feb 2005
Location: Braintree, UK
Posts: 1,165
Reputation:
Rep Power: 7
Solved Threads: 58
It's just a preference. I like things to be as loosley coupled as possible. I prefer to bind to collections of objects, rather than strongly typed datasets. Like most things it depends on what your requirements are. I wouldn't worry about it too much as this is your first real program.
I'm sure the presentation of the address can be done in a better way, using a bindingsource. When I have figured it out I'll post my solution here. (I do mostly web apps so I'm a bit rusty with forms where the platform is a little different)
I'm sure the presentation of the address can be done in a better way, using a bindingsource. When I have figured it out I'll post my solution here. (I do mostly web apps so I'm a bit rusty with forms where the platform is a little different)
•
•
Join Date: Nov 2006
Location: Bonners Ferry, ID
Posts: 279
Reputation:
Rep Power: 2
Solved Threads: 38
Instead of mangling the SQL statement (embedding CRLF), just iterate through the rows and columns, and populate the text box.
Here is a simple example:
// Jerry
Here is a simple example:
private void toolStripButton1_Click(object sender, EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=SHAWHP;Initial Catalog=SalonWiz;Integrated Security=True");
conn.Open();
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT top 10 * from Employee",conn);
adapter.Fill(ds);
foreach(DataRow row in ds.Tables[0].Rows)
{
for(int i=0;i< ds.Tables[0].Columns.Count;i++)
textBox1.Text += row[i].ToString()+Environment.NewLine;
textBox1.Text += Environment.NewLine;
}
}// Jerry
![]() |
•
•
•
•
•
•
•
•
DaniWeb C# Marketplace
•
•
•
•
Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
•
•
•
•
access advice ajax asp business code combo cult of the dead cow daniweb data data protection database developer development drive dropdownlist encryption europe forensic forensics government hacker hacking hard hardware help hitachi hp industrial espionage it microsoft module msdn net news office privacy protection reuse search security software sql storage survey terabyte tv vista web wikipedia
- Comboboxes as Lookups in vb6 (Visual Basic 4 / 5 / 6)
- How to search from a database, selecting from a combo or list box in vb? (Visual Basic 4 / 5 / 6)
- Display data in separate controls (SQL) (VB.NET)
- Textbox help (VB.NET)
- An unhandled exception occurred during the execution of the current web request. (ASP.NET)
- Populating & Retrieving Data in a listbox : ASP.NET (w/ VB.NET) (ASP.NET)
Other Threads in the C# Forum
- Previous Thread: how to read a csv file and sort the specific clolumn in descending order
- Next Thread: How to pivot a control from Right Side ?



Linear Mode