Good wonderful day! I have a SQLite database as follows. sqlite

I have the following C # code for reading data from the database:

private void playersCombobox_SelectionChanged(object sender, SelectionChangedEventArgs e) { string curr = playersCombobox.SelectedValue.ToString(); SQLiteConnection sqlConnection = new SQLiteConnection("Data Source=db.db"); SQLiteCommand sqlCmd = new SQLiteCommand("SELECT * FROM players WHERE nickname='"+curr+"';", sqlConnection); sqlConnection.Open(); SQLiteDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { ageLabel.Content = "Возраст: "+sqlReader["age"].ToString(); sexLabel.Content = "Пол: "+ sqlReader["sex"].ToString(); countryLabel.Content = "Страна: "+ sqlReader["country"].ToString(); } sqlReader.Close(); sqlConnection.Close(); } 

It works well and copes with its functions. However, if I want to take another base element from the SQliteReader, for example the "sniper" element, I get the following code:

 string curr = playersCombobox.SelectedValue.ToString(); SQLiteConnection sqlConnection = new SQLiteConnection("Data Source=db.db"); SQLiteCommand sqlCmd = new SQLiteCommand("SELECT * FROM players WHERE nickname='"+curr+"';", sqlConnection); sqlConnection.Open(); SQLiteDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { ageLabel.Content = "Возраст: "+sqlReader["age"].ToString(); sexLabel.Content = "Пол: "+ sqlReader["sex"].ToString(); countryLabel.Content = "Страна: "+ sqlReader["country"].ToString(); sniperLabel.Content = "Снайпер:" + sqlReader["sniper"].ToString(); } sqlReader.Close(); sqlConnection.Close(); 

As a result of code execution, I get an error:

error

Which leads to the line

 sniperLabel.Content = "Снайпер:" + sqlReader["sniper"].ToString(); 

Why is this happening and what am I doing wrong?

  • and try to rearrange the reading of the country and the sniper - vitidev
  • @vitidev it doesn’t solve my problem - crystal
  • then there are no more ideas. and this idea was that the reader does not materialize the line, but reads sequentially through the columns and cannot return back and having reached the country, he has already passed by the sniper - vitidev
  • one
    Have you tried to go through the debager code and see what you have there at all? Put a checkpoint in front of the line in which the error is displayed and run the program. When it reaches the point, select your sqlReader and press Shift + F10 (or PCM -> quickWatch window) and see what you have there at all. By mistake, there is no sniper in the sample and no sqlReader. - Alex Krass
  • Yes, of course I tried. However, all that shows me debager - the number of elements in SQLiteReader. There are 8 of them, exactly as in the base - crystal

2 answers 2

There are two possible causes of error.

  1. In SQLiteStudio, you are viewing one database in which there is a sniper column, and another database is connected in the application, in which there is no such column.
    This is a very common mistake during development: another database is connected to the application.

  2. A typo in the name of the column sniper: for example, the letter 'e' is Russian. Not finding the desired column, the provider throws an IndexOutOfRangeException.

  • You will not believe, but the first option was correct. - crystal

SELECT * FROM players Don't write like that. Always specify which fields you need in the sample. The error in the example you give indicates that the "sniper" is still missing in the resulting dataset. In addition, NULL by default and the unconditional call ToString () will obviously lead to another error.

Review the restrictions in the table. Rewrite the request.

 string curr = playersCombobox.SelectedValue.ToString(); SQLiteConnection sqlConnection = new SQLiteConnection("Data Source=db.db"); SQLiteCommand sqlCmd = new SQLiteCommand("SELECT TOP 1 age, sex, country, sniper FROM players WHERE nickname='"+curr+"';", sqlConnection); sqlConnection.Open(); SQLiteDataReader sqlReader = sqlCmd.ExecuteReader(); while (sqlReader.Read()) { ageLabel.Content = "Возраст: "+sqlReader[0].ToString(); sexLabel.Content = "Пол: "+ sqlReader[1].ToString(); countryLabel.Content = "Страна: "+ sqlReader[2].ToString(); sniperLabel.Content = "Снайпер:" + sqlReader[3].ToString(); } sqlReader.Close(); sqlConnection.Close(); 
  • Thank you for your reply. However, the error persists when explicitly specifying the fields in the request - crystal
  • If in more detail: an error, in your case indicates that the value you are trying to get by name is missing in the received sample. If this is not the case, then try specifying an alias in the query for the sniper field. - Alex
  • I honestly do not quite understand you. - crystal
  • try to do as I described in the added example - Alex
  • I get an error syntax error in SQL query - crystal