Get the column values from the SQLite database

Please support our C# advertiser: Programming Forums - DaniWeb Sister Site
Thread Solved

Join Date: May 2009
Posts: 30
Reputation: Krstevski is an unknown quantity at this point 
Solved Threads: 0
Krstevski's Avatar
Krstevski Krstevski is offline Offline
Light Poster

Get the column values from the SQLite database

 
0
  #1
Oct 31st, 2009
Hello guys, can someone tell me how can I get the values from the columns ?

My sql table is:

Table name: settbl
Table columns: id, first (int), second (int)

...and now, I want to get the values from "first" and "second", but I got the error message "Object reference no set to an instance of an object".

This is my code:

  1. sqlIteCon = new SQLiteConnection("Data Source=set.db;Version=3;New=False;Compress=True;");
  2. sqlIteCon.Open();
  3. string SelectQuery = "SELECT * FROM settbl";
  4. SQLiteCommand sqlCommand = new SQLiteCommand(SelectQuery, sqlIteCon);
  5.  
  6. SQLiteDataReader sqlRead = sqlCommand.ExecuteReader();
  7.  
  8. int a = sqlRead.GetInt32(sqlRead.GetOrdinal("first"));
  9.  
  10. int b = sqlRead.GetInt32(sqlRead.GetOrdinal("second"));

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: 633
Sponsor
sknake's Avatar
sknake sknake is offline Offline
.NET Enthusiast
 
0
  #2
Oct 31st, 2009
Why not load it in to a DataTable ?
  1. private void button9999_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 Where InvNumber = @InvNumber";
  5. using (SqlConnection conn = new SqlConnection(connStr))
  6. {
  7. conn.Open();
  8. using (SqlCommand cmd = new SqlCommand(query, conn))
  9. {
  10. cmd.Parameters.Add(new SqlParameter("@InvNumber", 1100));
  11. using (SqlDataReader dr = cmd.ExecuteReader())
  12. {
  13. using (DataTable dt = new DataTable())
  14. {
  15. dt.Load(dr);
  16. for (int i1 = 0; i1 < dt.Rows.Count; i1++)
  17. {
  18. DataRow row = dt.Rows[i1];
  19. string custName = Convert.ToString(row["CustName"]);
  20. string email = Convert.ToString(row["Email"]);
  21. }
  22. }
  23. }
  24. }
  25. conn.Close();
  26. }
  27. }
Scott Knake
Custom Software Development
Apex Software, Inc.
Reply With Quote Quick reply to this message  
Join Date: May 2009
Posts: 30
Reputation: Krstevski is an unknown quantity at this point 
Solved Threads: 0
Krstevski's Avatar
Krstevski Krstevski is offline Offline
Light Poster
 
0
  #3
Nov 1st, 2009
Sknake, thanks for your help/example, but I solved mu problem with the SQLiteDataReader Class

  1. public void ReadMyData(string myConnString)
  2. {
  3. string mySelectQuery = "SELECT DeptNo, DName FROM Dept";
  4. SQLiteConnection sqConnection = new SQLiteConnection(myConnString);
  5. SQLiteCommand sqCommand = new SQLiteCommand(mySelectQuery,sqConnection);
  6. sqConnection.Open();
  7. try
  8. {
  9. SQLiteDataReader sqReader = sqCommand.ExecuteReader();
  10. while (sqReader.Read())
  11. {
  12. Console.WriteLine(sqReader.GetInt32(0) + ", " + sqReader.GetString(sqReader.GetOrdinal("DName")));
  13. }
  14. sqReader.Close();
  15. }
  16. finally
  17. {
  18. sqConnection.Close();
  19. }
  20. }
Reply With Quote Quick reply to this message  
Reply

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



Other Threads in the C# Forum


Views: 404 | Replies: 2
Thread Tools Search this Thread



Tag cloud for C#
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC