Fill in the combo box with the value taken from the SQL Server CE table

enter image description here

I have been working on this basic form for a long time and hit my last stumbling block. The following still works: you enter the identifier in the text box, click the "Download" button, select .jpg , then display the image in the image window at the top (in this example, a video camera).

Now for this problem, when you click Save , a method is called with the name updatedata();

I believe that this is the image and identifier of my SQL Server CE database (I did this several times in the bin \ debug folder, and db grew in size). This method also calls a call to another method called Connection();

Now the idea of ​​the connection method is to fill in an ID code based on any elements that were saved in db, so basically every time I add a new image, the selection list should be updated and be available for selection, currently it does nothing, because the code I used was originally written for the corresponding instance of SQL Server, not CE. I tried to reorganize some of them, but now I get the following error.

enter image description here

Here is the code for my connection method:

 private void Connection() { //connect to the database and table //selecting all the columns //adding the name column alone to the combobox try { string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"; SqlCeConnection conn = new SqlCeConnection(connstr); conn.Open(); empadap1 = new SqlDataAdapter(); empadap1.SelectCommand = new SqlCommand("SELECT * FROM test_table" , conn); dset = new DataSet("dset"); empadap1.Fill(dset); DataTable dtable; dtable = dset.Tables[0]; comboBox1.Items.Clear(); foreach (DataRow drow in dtable.Rows) { comboBox1.Items.Add(drow[0].ToString()); comboBox1.SelectedIndex = 0; } } catch (Exception ex) { MessageBox.Show(ex.Message); } } 

Question
Can someone change the code of the Connection () method to do what I want (for example, update the drop-down list with all the identifier stored in the SQL Server CE database) or suggest a new code.

Side note
As soon as my combobox is full, I will try to program the "Restore" button based on the selected identifier, which will then display the selected image in the second image window under the first, but I think it's better if I save this separate problem!

+4
source share
3 answers

Try SqlCeDataAdapter and SqlCeCommand instead of SqlDataAdapter and SqlCommand

+1
source

I had the same question (about refreshment). I found one way, but not good, but it works. Just

 public ButtonSave_Click(...) { Connection(); Application.Restart(); } 

That was enough for me, but I don’t know if this will help you or not ...

0
source

try it

  private void Connection() { //connect to the database and table //selecting all the columns //adding the name column alone to the combobox try { string connstr = @"Data Source=.\TestImage.sdf;Persist Security Info=False;"; SqlCeConnection conn = new SqlCeConnection(connstr); conn.Open(); SqlCeCommand selectCmd = conn.CreateCommand(); selectCmd.CommandText = "SELECT * FROM test_table"; SqlCeDataAdapter adp = new SqlCeDataAdapter(selectCmd); dset = new DataSet("dset"); adp.Fill(dset); DataTable dtable; dtable = dset.Tables[0]; if(comboBox1.Items.Count>0) { comboBox1.Items.Clear(); } foreach (DataRow drow in dtable.Rows) { comboBox1.Items.Add(drow[0].ToString()); } comboBox1.SelectedIndex = 0; } catch (Exception ex) { MessageBox.Show(ex.Message); } finally { conn.Open(); } } 
0
source

Source: https://habr.com/ru/post/1485860/


All Articles