i am beginner for windows mobile 6 and iam trying to pass values to database but itz not working there is no errors and warnings please check my code and help me.. database not updating..
private void button1_Click(object sender, EventArgs e)
{
string conSTR = "data source= " +
(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
"\\DPass.sdf; Persist security info=False";
SqlCeConnection connection = new SqlCeConnection(conSTR);
SqlCeCommand cmd = new SqlCeCommand("INSERT INTO cusTable(Fname,Lname) VALUES(@Fname,@Lname)",connection);
cmd.Parameters.AddWithValue("@Fname", textBox1.Text);
cmd.Parameters.AddWithValue("@Lname", textBox2.Text);
connection.Open();
int affectedRows = cmd.ExecuteNonQuery();
cmd.ExecuteNonQuery();
MessageBox.Show("ela");
connection.Close();
}
Though not part of the solution, it’d be a good idea to move this outside of the loop so you don’t have to read this over and over.
Now, use Try/Catch statements to find out what you’ve done wrong.
In the modified code below, I’ve included 2 of these. The outer Try/Catch is set to
Exceptionand will swallow any exception. The inner Try/Catch is the one I suspect will throw the message, and it is anSqlCeException, which is only thrown for SqlCe Errors.It also appeared that you were calling your
INSERTfunction twice, so the secondExecuteNonQuery()call has been commented out.Run this code, and report back with what the error message is and whether the title for that MessageBox is “SqlCe Error” or “Non-SqlCe Error”.