I'm pretty new to C# and am struggling with the following query.
I'm trying to insert values into a database, but it won't accept null as a value. I need to differentiate between 0 and nothing in this field as it shows whether a calculation has occurred or not.
Here's what I have so far:
System.Data.OleDb.OleDbConnection objConn = new System.Data.OleDb.OleDbConnection();
var connStr = @"Provider=Microsoft.ACE.OLEDB.12.0;Data Source=Z:\Projects\database.accdb";
System.Data.OleDb.OleDbCommand ins = new System.Data.OleDb.OleDbCommand();
objConn.ConnectionString = connStr;
objConn.Open();
ins.Connection = objConn;
ins.CommandText = "INSERT INTO AutomatedDecisionProcess(item1, item2, item3) VALUES(@item1, @item2, @item3)";
ins.Parameters.AddWithValue("@item1", SolveBlanks(row[0]));
ins.Parameters.AddWithValue("@item2", SolveBlanks(row[1]));
ins.Parameters.AddWithValue("@item3", SolveBlanks(row[2]));
ins.ExecuteNonQuery();
objConn.Close();
ins.Dispose();
Console.WriteLine("success?");
and I am trying this function I've written:
public static string SolveBlanks(object number)
{
string answer = number.ToString();
if (answer == "")
{
answer = null;
}
return answer;
}
Any ideas?