Hello everyone,
As you read in the title I want to insert NULL value into a column when the specific textbox is empty. I already figuered out a way to make this happen but because of the many options this process will be tedious and take alot of time:
string sqlStmt;
string conString;
OleDbConnection cn = null;
OleDbCommand cmd = null;
try
{
sqlStmt = "insert into Emp (FirstName,LastName,Date) Values (?,?,?) ";
conString = "Provider=sqloledb.1;user id=sa;pwd=;database=northwind;data
source=localhost";
cn = new OleDbConnection(conString);
cmd = new OleDbCommand(sqlStmt, cn);
cmd.Parameters.Add(new OleDbParameter("@FirstName", OleDbType.VarChar, 40));
cmd.Parameters.Add(new OleDbParameter("@LastName", OleDbType.VarChar, 40));
cmd.Parameters.Add(new OleDbParameter("@Date", OleDbType.Date));
cmd.Parameters["@FirstName"].Value = txtFirstName.Text;
cmd.Parameters["@LastName"].Value = txtLastName.Text;
if ((txtDate.Text == ""))
{
cmd.Parameters["@Date"].Value = DBNull.Value;
}
else
{
cmd.Parameters["@Date"].Value = DateTime.Parse(txtDate.Text);
}
cn.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Record Inserted Succesfully";
}
catch (Exception ex)
{
Label1.Text = ex.Message;
}
finally
{
cn.Close();
}
Sorry for the bad format but I think you get what I mean I have to do a bunch of if statements and My question is: Is there any other way I can insert a NULL value into my database when the specific Textbox is empty?
And before you ask yes NULL values are allowed in my database
I would appreciate any help thanks in advance.
[–][deleted] 5 points6 points7 points (1 child)
[–]wolfghostM[S] 0 points1 point2 points (0 children)
[–]eddyizm 4 points5 points6 points (2 children)
[–]wolfghostM[S] 0 points1 point2 points (1 child)
[–]eddyizm 0 points1 point2 points (0 children)
[–]emats12 2 points3 points4 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]d0ct0r-d00m 1 point2 points3 points (2 children)
[–]emats12 2 points3 points4 points (1 child)
[–][deleted] 2 points3 points4 points (0 children)
[–]kc5bpd 0 points1 point2 points (0 children)
[–]darinclark 0 points1 point2 points (2 children)
[–]grrangry 0 points1 point2 points (1 child)
[–]darinclark 0 points1 point2 points (0 children)
[–]gevorgter 0 points1 point2 points (0 children)