Reputation: 10364
I've been working on this basic form for a while now and have hit my latest stumbling block.
What works so far is the following: You input an ID into the text box, Click the Load button, select your .jpg
this then displays the image in the picture box at the top (in this example its a camcorder).
Now for the problem, when you click the Save
button it makes a call to a method named updatedata();
This I believe is writing the image and ID to my SQL Server CE database (I have done it a few times in the bin\debug folder and the db has grown in size.) The method also makes a call to another method named Connection();
Now, the idea of the connection method is to populate the ID combobox based on any items that have been saved to the db, so basically every time I add a new image, the picklist should refresh and be available for selection, currently it is not doing anything because the code I used was initially written for a proper instance of SQL Server, not CE. I have attempted to refactor some of it however I am now getting the below error.
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 anyone ammend my Connection() method code to do what I want (i.e. update the combobox with all saved ID's found within the SQL Server CE database) or suggest some new code.
Side Note
Once my combobox is populating I will attempt to code the 'Retrieve' button based on the selected ID which will then display the selected image in a second picture box below the first one however I think it is best if I keep that a separate issue!
Upvotes: 4
Views: 3108
Reputation: 457
try this
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();
}
}
Upvotes: 0
Reputation: 1249
I had the same question(about refreshing). I've found one way, not good but works. Simply
public ButtonSave_Click(...)
{
Connection();
Application.Restart();
}
That was enough for me but I don't know will it help you or not...
Upvotes: 0
Reputation: 941
Try SqlCeDataAdapter
and SqlCeCommand
instead of SqlDataAdapter
and SqlCommand
Upvotes: 1