Reputation: 29
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();
}
Upvotes: 0
Views: 796
Reputation:
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.
private readonly string CON_STR = "data source= " +
(System.IO.Path.GetDirectoryName(System.Reflection.Assembly.GetExecutingAssembly().GetName().CodeBase)) +
"\\DPass.sdf; Persist security info=False";
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 Exception
and will swallow any exception. The inner Try/Catch is the one I suspect will throw the message, and it is an SqlCeException
, which is only thrown for SqlCe Errors.
private void button1_Click(object sender, EventArgs e)
{
int affectedRows = 0;
string sqlText = "INSERT INTO cusTable(Fname,Lname) VALUES(@Fname,@Lname)";
try {
using (var cmd = new SqlCeCommand(sqlText, new SqlCeConnection(CON_STR))) {
cmd.Parameters.AddWithValue("@Fname", textBox1.Text);
cmd.Parameters.AddWithValue("@Lname", textBox2.Text);
try {
cmd.Connection.Open();
affectedRows = cmd.ExecuteNonQuery();
//cmd.ExecuteNonQuery(); <= ?? Why are you calling this twice?
} catch (SqlCeException ceEr) {
MessageBox.Show(ceEr.Message, "SqlCe Error");
} finally {
cmd.Connection.Close();
}
}
} catch (Exception err) {
MessageBox.Show(err.Message, "Non-SqlCe Error");
}
MessageBox.Show(string.Format("Affected Rows: {0}", affectedRows), "ela");
}
It also appeared that you were calling your INSERT
function twice, so the second ExecuteNonQuery()
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".
Upvotes: 1
Reputation: 5959
although I did not yet use your way to insert data in SQL CE databases it looks like your query string is missing some items. Looking at http://msdn.microsoft.com/en-us/library/aa977880%28v=vs.71%29.aspx I see you are using this syntax:
INSERT INTO employee (emp_no, fname, lname, officeno) ;VALUES (3022, "John", "Smith", 2101)
But in your query the semicolon is missing after the field list.
Secondly, if you need to insert strings info a db these must be surrounded by quotes.
So please try the follwoing query string:
"INSERT INTO cusTable(Fname,Lname); VALUES(\"@Fname\",\"@Lname\")"
BTW: if something with an SQL statement in code does not work, I use MS Access or SQL Manager to test my query string.
regards
Josef
Upvotes: 0