0

I am making a C# Winform and I am to the point where I read some data from xml, and from interaction with the user. I then need to put that data into a local database and then read it trough a sub-form.

I am declaring, and reading the data:

public int score = 0; //not null after user interaction
public string candidate = null; //not null after user interaction
XElement Title = Test.Root.Element("title");
XElement MaxScore = Test.Root.Element("property_maxscore");
XElement PassScore = Test.Root.Element("property_passscore");

( until here all works I just post it for better code comprehence )

Now I try to insert some data into the local database table, I have used two approaches sadly both of them did not worked for me:

First:

SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");
con.Open();
SqlCeCommand cmd = new SqlCeCommand(@"INSERT INTO final_tests (test_name, candidate, score, pass_score, max_score, completed) VALUES (@test_name, @candidate, @score, @pass_score, @max_score, @completed)", con);
cmd.Parameters.AddWithValue("test_name", (string) Title);
cmd.Parameters.AddWithValue("candidate", candidate);
cmd.Parameters.AddWithValue("score", score);
cmd.Parameters.AddWithValue("pass_score", (string) PassScore);
cmd.Parameters.AddWithValue("max_score", (string) MaxScore);
cmd.Parameters.AddWithValue("completed", DateTime.Now);
cmd.ExecuteNonQuery();
con.Close();

Second:

SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");
con.Open();
using (SqlCeCommand comm = new SqlCeCommand("SELECT * FROM final_tests WHERE score LIKE @score", con))
{
comm.Parameters.Add(new SqlCeParameter("score", score));
comm.ExecuteNonQuery();
}

I test each and every line and they execute successfully, but when I manually open the database is empty and of course the DataGridView in the other sub-forms are empty too.

I have tried many modifications but unfortunately, none worked. I can not find where my mistake is. If more information is needed I am here. To finish my question: Data from xml and user interaction is read and then must be put into the mentioned local database, saved for later use and displayed in DataGridView in other sub-forms. Thank you all !

2
  • check what cmd.ExecuteNonQuery() returns. It should return number of affected rows (msdn.microsoft.com/en-us/library/…)
    – vidriduch
    Feb 26, 2016 at 11:14
  • What exactly do you expect that second query to do? It's a SELECT, but you never do anything with the results.
    – David
    Feb 26, 2016 at 11:15

2 Answers 2

1

SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");

con.Open();
SqlCeCommand cmd = new SqlCeCommand(@"INSERT INTO final_tests (test_name, candidate, score, pass_score, max_score, completed) VALUES (@test_name, @candidate, @score, @pass_score, @max_score, @completed)", con);
cmd.Parameters.AddWithValue("@test_name", (string) Title);
cmd.Parameters.AddWithValue("@candidate", candidate);
cmd.Parameters.AddWithValue("@score", score);
cmd.Parameters.AddWithValue("@pass_score", (string) PassScore);
cmd.Parameters.AddWithValue("@max_score", (string) MaxScore);
cmd.Parameters.AddWithValue("@completed", DateTime.Now);
int i=cmd.ExecuteNonQuery();


     if(i>0)
    {
         // Success
    }
    con.Close();

And second Correction

SqlCeConnection con = new SqlCeConnection(@"Data Source=...users.sdf");
con.Open();
using (SqlCeCommand comm = new SqlCeCommand("SELECT * FROM final_tests WHERE score LIKE @score", con))
{
comm.Parameters.Add(new SqlCeParameter("score", score));
SsqlDataAdapter aa= new SsqlDataAdapter(Comm);
DataTable dt= new Datatable();
da.Fill(dt);
}
0
0

Thank you all for the help and guideline ! And mostly thank you Tanmay Nehete. Your first correction worked like a charm.

I have added @ before the names of the columns:

cmd.Parameters.AddWithValue("@test_name", (string) Title);

and the line:

int i=cmd.ExecuteNonQuery();

and the data is stored in the local database and is displayed in all the DataGridViews THANK YOU !

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.