Hello Guys!
I'm trying to update my SQL table in C#. I've been struggling with updating the table for a couple hours now, and I've searched so much, I can't wrap my head around what is wrong.
The focus is on the IndtastBeløb method, im trying to determine if there is an entry in the database for the current date, if there is I want to update that row. Everything in the code works until I'm trying to update with the changeRow.ExecuteNonQuery(); The program doesn't throw an exception or give me any errors, the database simply does not change :)
I hope that someone out there can help me! :)
I'm trying to update my SQL table in C#. I've been struggling with updating the table for a couple hours now, and I've searched so much, I can't wrap my head around what is wrong.
Code
public class SQLConnect
{
public SQLConnect(string startUp)
{
startupPath = startUp;
connectionSuccesful = false;
OpenConnection();
}
public SqlConnection sqlConnect;
public string startupPath { get; set; }
public bool connectionSuccesful { get; set; }
public bool temp { get; set; }
public void IndtastBeløb(int beløb, string kategori)
{
DateTime time = DateTime.Now;
int iBolig = 0; int iOther = 0; int iTransport = 0; int iLoan = 0; int iMad = 0; int iDiverse = 0;
SqlCommand command = new SqlCommand("SELECT * FROM Entries WHERE dag = @day AND måned = @month AND år = @year", sqlConnect); //
command.Parameters.AddWithValue("@day", time.Day);
command.Parameters.AddWithValue("@month", time.Month);
command.Parameters.AddWithValue("@year", time.Year);
/*command.Parameters.Add("@day", SqlDbType.Int);
command.Parameters["@day"].Value = time.Day;
command.Parameters.Add("@month", SqlDbType.Int);
command.Parameters["@month"].Value = time.Month;
command.Parameters.Add("@year", SqlDbType.Int);
command.Parameters["@year"].Value = time.Year;*/
SqlDataReader reader = command.ExecuteReader();
if (reader.HasRows)
{
while (reader.Read())
{
//thing = reader["bolig"].ToString();
//int iBolig = reader.GetInt32(0);
iBolig = (int)reader["bolig"];
iOther = (int)reader["øvrige"];
iTransport = (int)reader["transport"];
iLoan = (int)reader["gæld"];
iMad =(int)reader["mad"];
iDiverse = (int)reader["diverse"];
switch (kategori)
{
case "Bolig":
iBolig += beløb;
break;
case "Øvrige":
iOther += beløb;
break;
case "Transport":
iTransport += beløb;
break;
case "gæld":
iLoan += beløb;
break;
case "mad":
iMad += beløb;
break;
case "diverse":
break;
default:
break;
}
}
reader.Close();
SqlCommand changeRow = new SqlCommand("UPDATE Entries SET bolig=@bolig WHERE dag=@day", sqlConnect); //, øvrige=@øvrige, transport=@transport, gæld=@gæld, mad=@mad, diverse=@diverse " + "WHERE dag=@day AND måned=@month AND år=@year"
changeRow.Parameters.AddWithValue("@bolig", iBolig);
changeRow.Parameters.AddWithValue("@day", time.Day);
/*changeRow.Parameters.AddWithValue("@øvrige", iOther);
changeRow.Parameters.AddWithValue("@transport", iTransport);
changeRow.Parameters.AddWithValue("@gæld", iLoan);
changeRow.Parameters.AddWithValue("@mad", iMad);
changeRow.Parameters.AddWithValue("@diverse", iDiverse);
changeRow.Parameters.AddWithValue("@month", time.Month);
changeRow.Parameters.AddWithValue("@year", time.Year);*/
changeRow.ExecuteNonQuery();
}
else
{
temp = false;
}
}
public void OpenConnection()
{
sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='" + startupPath + @"LuksusDatabase.mdf';Integrated Security=True");
//sqlConnect = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename='c:\users\simo8211\documents\visual studio 2015\Projects\LuksusFældenForms\LuksusFældenForms\bin\Debug\LuksusDatabase.mdf';Integrated Security=True");
try
{
sqlConnect.Open();
connectionSuccesful = true;
}
catch
{
connectionSuccesful = false;
}
}
}The focus is on the IndtastBeløb method, im trying to determine if there is an entry in the database for the current date, if there is I want to update that row. Everything in the code works until I'm trying to update with the changeRow.ExecuteNonQuery(); The program doesn't throw an exception or give me any errors, the database simply does not change :)
I hope that someone out there can help me! :)



![[Image: MirCc8E.png]](https://i.imgur.com/MirCc8E.png)
![[Image: IQE6i8G.png]](https://i.imgur.com/IQE6i8G.png)
