EDN Admin
Well-known member
Hi, I am new to c# and working on a simple(ish) project which will allow an end user to enter a Manufacturers name and model in to textbox controls, the values of which will be inserted in to two related tables using a stored procedure. The problem
is if a textbox value is left blank, it inserts the blank value in to the dbo even though the table does not allow nulls - what am I missing?
Could someone help me code something which would prevent this, am I right in thinking an if/else statement would be required?
My code so far is
<pre>private void btnSave_Click(object sender, EventArgs e)
{
try
{
connection.ConnectionString = " etc, etc";
command.Connection = connection;
command.CommandText = "InsertModelManufacturer";
command.CommandType = CommandType.StoredProcedure;
SqlParameter modelParameter = new SqlParameter();
modelParameter.ParameterName = "@ModelName";
modelParameter.SqlDbType = SqlDbType.VarChar;
modelParameter.Direction = ParameterDirection.Input;
modelParameter.Value = txtModel.Text;
SqlParameter manufacturerParameter = new SqlParameter();
manufacturerParameter.ParameterName = "@ManufacturerName";
manufacturerParameter.SqlDbType = SqlDbType.VarChar;
manufacturerParameter.Direction = ParameterDirection.Input;
manufacturerParameter.Value = txtManufacturer.Text;
command.Parameters.Add(manufacturerParameter);
command.Parameters.Add(modelParameter);
connection.Open();
if (connection.State == ConnectionState.Open)
{
int rows = command.ExecuteNonQuery();
if (rows > 0)
MessageBox.Show("Record Saved");
else
MessageBox.Show("Failed to save record");
}
}
catch (SqlException ex)
{
MessageBox.Show("An error has occured!" + ex);
}
finally
{connection.Close();
}[/code]
<br/>
<br/>
View the full article
is if a textbox value is left blank, it inserts the blank value in to the dbo even though the table does not allow nulls - what am I missing?
Could someone help me code something which would prevent this, am I right in thinking an if/else statement would be required?
My code so far is
<pre>private void btnSave_Click(object sender, EventArgs e)
{
try
{
connection.ConnectionString = " etc, etc";
command.Connection = connection;
command.CommandText = "InsertModelManufacturer";
command.CommandType = CommandType.StoredProcedure;
SqlParameter modelParameter = new SqlParameter();
modelParameter.ParameterName = "@ModelName";
modelParameter.SqlDbType = SqlDbType.VarChar;
modelParameter.Direction = ParameterDirection.Input;
modelParameter.Value = txtModel.Text;
SqlParameter manufacturerParameter = new SqlParameter();
manufacturerParameter.ParameterName = "@ManufacturerName";
manufacturerParameter.SqlDbType = SqlDbType.VarChar;
manufacturerParameter.Direction = ParameterDirection.Input;
manufacturerParameter.Value = txtManufacturer.Text;
command.Parameters.Add(manufacturerParameter);
command.Parameters.Add(modelParameter);
connection.Open();
if (connection.State == ConnectionState.Open)
{
int rows = command.ExecuteNonQuery();
if (rows > 0)
MessageBox.Show("Record Saved");
else
MessageBox.Show("Failed to save record");
}
}
catch (SqlException ex)
{
MessageBox.Show("An error has occured!" + ex);
}
finally
{connection.Close();
}[/code]
<br/>
<br/>
View the full article