C# SQL Insert just won't work

Tuesday, July 8, 2014

I'm programming a control panel here and I've stumbled into a problem which I just can't seem to resolve.


I have a very simple .sdf database with two tables, Players and Teams.


[Players] has the following fields: id, nimi, nimimerkki, ika, kuva_polku.


[Teams} has: id, nimi, kansallisuus, kuva_polku.


I'm establishing connection to it for inserting, deleting and updating data. I've gotten this to work on the Players table with no problems, when when I did the same with minor changes to the Teams, for some reason I get no errors but the data just won't insert itself onto the table.


Here's the code:



SqlCeConnection conn = new SqlCeConnection(@"Data Source=" + AppDomain.CurrentDomain.BaseDirectory + "Players.sdf");
SqlCeCommand cmd = new SqlCeCommand();
SqlCeDataReader dr;


Code for inserting data onto the table Players:



// Insert a player into a database
private void insertPlayer(string name, string nickname, string age, string imagepath)
{
try
{
if (name != "" & nickname != "" & age != "" & imagepath != "")
{
if (conn != null && conn.State == ConnectionState.Closed)
{
conn.Open();
}
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Players (nimi, nimimerkki, ika, kuva_polku) values ('" + name + "','" + nickname + "','" + age + "','" + imagepath + "')";
cmd.ExecuteNonQuery();

MessageBox.Show("Tietue tallennettu tietokantaan");

nickname = "";
name = "";
age = "";
imagepath = "";
}
else
{
MessageBox.Show("Et täyttänyt kaikkia kenttiä");
}

}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
conn.Close();
cmd.Connection.Close();
}
}


Then the code for teams. I've stripped some stuff from it when I tried to search for the error here.



// Inserts a team into the database
private void insertTeam(string name, string nationality, string imagepath)
{
try
{
if (conn != null && conn.State == ConnectionState.Closed)
{
conn.Open();
}

cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Teams (nimi, kansallisuus, kuva_polku) values ('" + name + "','" + nationality + "','" + imagepath + "')";
cmd.ExecuteNonQuery();

MessageBox.Show("Tietue tallennettu tietokantaan");
}
catch (Exception ex)
{
MessageBox.Show(ex.Message, "Error");
}
finally
{
conn.Close();
cmd.Connection.Close();
}
}


Then I have the buttons, which in Players takes the data from textboxes and works fine. I tried the same with Teams before I downgraded it to just string so I could maybe pinpoint the error, but no.



private void button9_Click(object sender, EventArgs e)
{
insertPlayer(tbPlayerNameLeft.Text, tbPlayerNickLeft.Text, tbPlayerAgeLeft.Text, tbPlayerImagePathLeft.Text);
}

private void btnInsertTeamLeft_Click(object sender, EventArgs e)
{
insertTeam("Tiimin nimi","Tiimin kansallisuus","Tiimin logopolku");
}


The table seems to work fine when I write some SQL querys via Visual Studio etc. but I just don't know what the problem here is. These two tables and codes are nearly identical and still it just won't work.







http://ift.tt/1mbfY6L