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

Trouble obtaining database table values...

Hi, I need some suggestions...

I have created a form in visual studio 2005 in which a user chooses between two dates. I placed two dateTimePickers and a View Button. When the user clicks the view button, I have a sql command that is supposed to retrieve all values from both table 'Guest' and table 'Sales' in sql server 2005.
My code looks like this:
(c-sharp)
Expand|Select|Wrap|Line Numbers
  1. SqlConnection dbConnection = new SqlConnection(xmldb);
  2.             SqlCommand cmd = new SqlCommand(" SELECT * From Guest g, Sales s Where s.Date >= '" + dateTimePicker1.Value + "' And s.Date <= '" + dateTimePicker2.Value + "' Order By s.Date ", dbConnection1);                                    
  3.             cmd.CommandType = CommandType.Text;
  4.             SqlDataAdapter da = new SqlDataAdapter(cmd);             
  5.             DataTable dt = new DataTable();
  6.             da.Fill(dt);
  7.             textBox1.Text = dt.Rows[0][0].ToString();
My obvious problem here is that since I want all values from both tables, I don't know what other process I should use. For example:
textBox1.Text = dt.Rows[0][0].ToString(); will not work because it will only return the number of rows...The other problem is that I have tested the function with actual dates and it works fine, so it has something to do with dateTimePickers1.Text or something else...I would really appreciate some suggestions as to how to obtains all values and details found on those two tables between those two dates and on what would be the easiest way to display it. Thanks. Any input as to obtain all the values will be greatly appreaciated...

--GM
Jan 22 '08 #1
11 1763
Is there any way to retrieve all the info from two tables into a textBox? has anyone ever tried something like it?....
Jan 22 '08 #2
ck9663
2,878 Expert 2GB
Hi, I need some suggestions...

I have created a form in visual studio 2005 in which a user chooses between two dates. I placed two dateTimePickers and a View Button. When the user clicks the view button, I have a sql command that is supposed to retrieve all values from both table 'Guest' and table 'Sales' in sql server 2005.
My code looks like this:
(c-sharp)
Expand|Select|Wrap|Line Numbers
  1. SqlConnection dbConnection = new SqlConnection(xmldb);
  2.             SqlCommand cmd = new SqlCommand(" SELECT * From Guest g, Sales s Where s.Date >= '" + dateTimePicker1.Value + "' And s.Date <= '" + dateTimePicker2.Value + "' Order By s.Date ", dbConnection1);                                    
  3.             cmd.CommandType = CommandType.Text;
  4.             SqlDataAdapter da = new SqlDataAdapter(cmd);             
  5.             DataTable dt = new DataTable();
  6.             da.Fill(dt);
  7.             textBox1.Text = dt.Rows[0][0].ToString();
My obvious problem here is that since I want all values from both tables, I don't know what other process I should use. For example:
textBox1.Text = dt.Rows[0][0].ToString(); will not work because it will only return the number of rows...The other problem is that I have tested the function with actual dates and it works fine, so it has something to do with dateTimePickers1.Text or something else...I would really appreciate some suggestions as to how to obtains all values and details found on those two tables between those two dates and on what would be the easiest way to display it. Thanks. Any input as to obtain all the values will be greatly appreaciated...

--GM
is it ok for you to post the structure of these two tables?

-- CK
Jan 22 '08 #3
is it ok for you to post the structure of these two tables?

-- CK
Sure...Here it is below, sorry for the size...this thing doesn't allow any bigger.
Attached Images
File Type: jpg GuestNSales.JPG (12.9 KB, 166 views)
Jan 23 '08 #4
Hi, since I'm not sure how to obtain all values (select*), I'ved decided to select the columns I need. When I want to display them on the textBox my code looks like this:
Expand|Select|Wrap|Line Numbers
  1. textBox1.Text = dt1.Rows[0]["GuestFirstName"].ToString();                
  2. textBox1.Text += dt1.Rows[0]["GuestLastName"].ToString();                
  3. textBox1.Text += dt1.Rows[0]["GuestAddress"].ToString();                
  4. textBox1.Text += dt1.Rows[0]["Country"].ToString();                
  5. textBox1.Text += dt1.Rows[0]["GuestPhone"].ToString();                
  6. textBox1.Text += dt1.Rows[0]["GuestEmail"].ToString();                
  7. textBox1.Text += dt1.Rows[0]["SaleID"].ToString();                
  8. textBox1.Text += dt1.Rows[0]["SaleTotal"].ToString();                
  9. textBox1.Text += dt1.Rows[0]["InvoiceNumber"].ToString();                
  10. textBox1.Text += dt1.Rows[0]["SaleItem"].ToString();                
  11. textBox1.Text += dt1.Rows[0]["PaymentType"].ToString();                
  12. textBox1.Text += dt1.Rows[0]["SaleDiscount"].ToString();                
  13. textBox1.Text += dt1.Rows[0]["SalesStatus"].ToString();
  14. textBox1.Text += dt1.Rows[0]["Date"].ToString();
  15. textBox1.Text += dt1.Rows[0]["Quantity"].ToString();
  16. textBox1.Text += dt1.Rows[0]["SalePrice"].ToString();
But this method will only get me the first row, what can I do to get all the rows that are (n) number. Also the values are not spaced out, any links to a similar problem will be great.
Jan 23 '08 #5
Ok I do get all the rows with that method, the only issue is that it's too tedious when it comes to hundreds of sales in between those dates. I would need to enter each row a hundred times and still don't know if it will be spacious enough to display the info in. Is there an easier method to display all the info? Anyone??
Jan 23 '08 #6
ck9663
2,878 Expert 2GB
Ok I do get all the rows with that method, the only issue is that it's too tedious when it comes to hundreds of sales in between those dates. I would need to enter each row a hundred times and still don't know if it will be spacious enough to display the info in. Is there an easier method to display all the info? Anyone??
you're right. it does not get any bigger than that. could you just write here the structure, please.

-- ck
Jan 23 '08 #7
you're right. it does not get any bigger than that. could you just write here the structure, please.

-- ck
Ok. The columns I need from Guest are these:
GuestFirstName, GuestLastName, GuestAddress, Country, GuestPhone, and GuestEmail.

From Sales are these:
SaleTotal, InvoiceNumber, SaleItem, PaymentType, SaleDiscount, SalesStatus, Date, Quantity, SalePrice

so basically i have this select statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleID,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.SalesStatus,s.Date,s.Quantity,s.SalePrice From Guest g, Sales s Where g.GuestID = s.GuestID And s.Date Between '" + dateTimePicker1.Value + "' And '" + dateTimePicker2.Value + "' Order By s.Date
but I still don't know how to display it properly on a single textBox.
Jan 23 '08 #8
ck9663
2,878 Expert 2GB
Ok. The columns I need from Guest are these:
GuestFirstName, GuestLastName, GuestAddress, Country, GuestPhone, and GuestEmail.

From Sales are these:
SaleTotal, InvoiceNumber, SaleItem, PaymentType, SaleDiscount, SalesStatus, Date, Quantity, SalePrice

so basically i have this select statement:
Expand|Select|Wrap|Line Numbers
  1. SELECT g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail,s.SaleID,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.SalesStatus,s.Date,s.Quantity,s.SalePrice From Guest g, Sales s Where g.GuestID = s.GuestID And s.Date Between '" + dateTimePicker1.Value + "' And '" + dateTimePicker2.Value + "' Order By s.Date
but I still don't know how to display it properly on a single textBox.
you want the entire record in a single textbox? it's not really wrong, but it'll be unreadable. here goes:

first try:

SELECT g.GuestFirstName+g.GuestLastName+g.GuestAddress+g. Country+g.GuestPhone+g.GuestEmail+s.SaleID+s.SaleT otal+s.InvoiceNumber+s.SaleItem+s.PaymentType+s.Sa leDiscount+s.SalesStatus+s.Date+s.Quantity+s.SaleP rice as singleresult From Guest g, Sales s Where g.GuestID = s.GuestID And s.Date Between '" + dateTimePicker1.Value + "' And '" + dateTimePicker2.Value + "' Order By s.Date

it will return a single field called "singleresult". on your GUI side, you will need a datagrid viewer or you might want to do a DO UNTIL RecordSet.EOF = true and display each rows.

-- ck
Jan 23 '08 #9
I tested it to see if it would work in sql server but it displays this error:
Expand|Select|Wrap|Line Numbers
  1. Conversion failed when converting the varchar value 'MarioBetancourt14 Hopkins Streetbelize123mariobet@bowen.bz' to data type int.
  2.  
Jan 23 '08 #10
ck9663
2,878 Expert 2GB
I tested it to see if it would work in sql server but it displays this error:
Expand|Select|Wrap|Line Numbers
  1. Conversion failed when converting the varchar value 'MarioBetancourt14 Hopkins Streetbelize123mariobet@bowen.bz' to data type int.
  2.  
for all non-char fields, you need to cast it...do a CAST(ColumnName as varchar(50))

-- ck
Jan 23 '08 #11
for all non-char fields, you need to cast it...do a CAST(ColumnName as varchar(50))

-- ck
Ok kool, hey i found an easier way to do it. I ended up using two datagridviews. One displaying the Guest info and the next displaying the sales info.
Expand|Select|Wrap|Line Numbers
  1.             SqlConnection dbConnection1 = new SqlConnection(xmldb);                
  2.             SqlDataAdapter da1 = new SqlDataAdapter("Select g.GuestID,g.GuestFirstName,g.GuestLastName,g.GuestAddress,g.Country,g.GuestPhone,g.GuestEmail from Guest as g", dbConnection1);
  3.             DataSet ds1 = new DataSet();            
  4.             da1.Fill(ds1, "Guest");
  5.             SqlDataAdapter da2 = new SqlDataAdapter("Select s.GuestID,s.SaleTotal,s.InvoiceNumber,s.SaleItem,s.PaymentType,s.SaleDiscount,s.SalesStatus,s.Date,s.Quantity,s.SalePrice From Sales s Where s.Date Between '" + dateTimePicker1.Value + "' And '" + dateTimePicker2.Value + "' Order By s.Date ", dbConnection1);
  6.             da2.Fill(ds1, "Sales");
  7.  
  8.             DataRelation rel = new DataRelation
  9.               ("Sales for this Guest",
  10.                ds1.Tables["Guest"].Columns["GuestID"],
  11.                ds1.Tables["Sales"].Columns["GuestID"]);
  12.             ds1.Relations.Add(rel);
  13.                 dataGridView1.DataSource = ds1;
  14.                 dataGridView1.DataMember = "Guest";
  15.                 dataGridView2.DataSource = ds1;
  16.                 dataGridView2.DataMember = "Guest.Sales for this Guest";
Thanks Again, Apreciate it

--GM
Jan 23 '08 #12

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

Similar topics

9
by: Penn Markham | last post by:
Hello all, I am writing a script where I need to use the system() function to call htpasswd. I can do this just fine on the command line...works great (see attached file, test.php). When my...
14
by: Ian Rastall | last post by:
Sorry for the double question. I'm having a terrible time figuring out how to escape apostrophes in my mySQL database. Perhaps they have to be escaped in the PHP, using mysql_real_escape_string? ...
9
by: Roger Withnell | last post by:
I'm inserting a new record into an MS SQL database table and I want to obtain the new records autonumber immediately afterwards, as follows: MadminRS.CursorLocation = adUseServer...
5
by: Lyn | last post by:
I am trying to copy selected fields from one table to another, by reading the source table record into a recordset (ADO) and then copying the relevant fields to an SQL statement "INSERT INTO...". ...
59
by: Rico | last post by:
Hello, I have an application that I'm converting to Access 2003 and SQL Server 2005 Express. The application uses extensive use of DAO and the SEEK method on indexes. I'm having an issue when...
11
by: seannakasone | last post by:
Is there a way to get the callstack level in c++? for example, take the following code: void call3() { //callstack level would be 3 } void call2() { //callstack level would be 2 call3();
6
by: Ted | last post by:
Here is one such function: CREATE FUNCTION my_max_market_date () RETURNS datetime BEGIN DECLARE @mmmd AS datetime; SELECT max(h_market_date) INTO @mmmd FROM holdings_tmp; RETURN @mmmd; END ...
6
by: gggram2000 | last post by:
Hi, I need some suggestions... I have created a form in visual studio 2005 in which a user chooses between to dates. I placed two dateTimePickers and a View Button. When the user clicks the view...
9
by: raamay | last post by:
I have six checkboxes as shown below: <table> <tr> <td><input name="spec1" type="checkbox" value="0" tabindex="11" /><label id="label">Bridge Construction</label></td> </tr> <tr> <td><input...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
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: 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: 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
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
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...
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...

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.