Store data in variables

Please support our VB.NET advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster

Store data in variables

 
0
  #1
Oct 22nd, 2009
Hi Guys,

I am using sql in and am stuck. after "selecting * from table", how do i store whatever I have obtained from my database in a variable, for display?

Thanks.
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,473
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 22nd, 2009
Post the code where you are executing the select * from table . There are a number of ways to go about this.
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster
 
0
  #3
Oct 22nd, 2009
OK, am having something like this:
  1. .............................
  2. .............................
  3. Dim SQLConn As New SqlConnection
  4. Dim SQLCmd As New SqlCommand
  5. SQLConn.ConnectionString = ConnStr
  6. SQLConn.Open()
  7. SQLStr = "use Institution" 'This is my database
  8. SQLCmd.CommandText = SQLStr
  9. SQLCmd.Connection = SQLConn
  10. SQLCmd.ExecuteNonQuery()
  11. SQLStr = "Select * from Stud_Details where 1"
  12. SQLCmd.CommandText = SQLStr
  13. SQLCmd.Connection = SQLConn
  14. SQLCmd.ExecuteNonQuery() 'The problem begins here! Is it OK?
Reply With Quote Quick reply to this message  
Join Date: Feb 2009
Posts: 3,473
Reputation: sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of sknake has much to be proud of 
Solved Threads: 630
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #4
Oct 22nd, 2009
No .. that isn't ok. You need to load the results in to a DataTable using a SqlDataReader created by calling SqlCommand.ExecuteReader() .

Example:
  1. private void button3333_Click(object sender, EventArgs e)
  2. {
  3. const string connStr = "Data Source=apex2006sql;Initial Catalog=Leather;Integrated Security=True;";
  4. const string query = "Select * From Invoice (NOLOCK)";
  5.  
  6. DataTable dt;
  7.  
  8. using (SqlConnection conn = new SqlConnection(connStr))
  9. {
  10. conn.Open();
  11. using (SqlCommand cmd = new SqlCommand(query, conn))
  12. {
  13. using (SqlDataReader dr = cmd.ExecuteReader())
  14. {
  15. dt = new DataTable();
  16. dt.Load(dr); //This loads a table
  17. }
  18. }
  19. conn.Close();
  20. }
  21.  
  22. MessageBox.Show(string.Format("You have {0:F0} rows", dt.Rows.Count));
  23. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: Sep 2009
Posts: 18
Reputation: SecurExpert is an unknown quantity at this point 
Solved Threads: 1
SecurExpert SecurExpert is offline Offline
Newbie Poster
 
0
  #5
Oct 23rd, 2009
thanks alot, it just worked.
thanks for your help.

regards
Reply With Quote Quick reply to this message  
Reply

This thread has been marked solved.
Perhaps start a new thread instead?
Message:




Views: 314 | Replies: 4
Thread Tools Search this Thread



Tag cloud for VB.NET
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC