944,126 Members | Top Members by Rank

Ad:
  • C# Discussion Thread
  • Marked Solved
  • Views: 1746
  • C# RSS
Oct 31st, 2009
0

Get the column values from the SQLite database

Expand Post »
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:

C# Syntax (Toggle Plain Text)
  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...
Reputation Points: 17
Solved Threads: 5
Junior Poster
Krstevski is offline Offline
110 posts
since May 2009
Oct 31st, 2009
0
Re: Get the column values from the SQLite database
Why not load it in to a DataTable ?
C# Syntax (Toggle Plain Text)
  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. }
Featured Poster
Reputation Points: 1749
Solved Threads: 735
Senior Poster
sknake is offline Offline
3,948 posts
since Feb 2009
Nov 1st, 2009
0
Re: Get the column values from the SQLite database
Sknake, thanks for your help/example, but I solved mu problem with the SQLiteDataReader Class

C# Syntax (Toggle Plain Text)
  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. }
Reputation Points: 17
Solved Threads: 5
Junior Poster
Krstevski is offline Offline
110 posts
since May 2009

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C# Forum Timeline: Voice Streaming
Next Thread in C# Forum Timeline: WPF WebBrowser Control





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC