I have multiple textboxes and I want to allow the user to leave some empty instead of giving the error
SqlParameterCollection only accepts non-null SqlParameter type objects.
What I use for inserting data that is in Data_Access_Layer Class
public void ExecuteCommand(string Data, SqlParameter[] param)
{
SqlCommand sqlcmd = new SqlCommand();
sqlcmd.CommandType = CommandType.StoredProcedure;
sqlcmd.CommandText = Data;
sqlcmd.Connection = sqlconnection;
if (param != null)
{
sqlcmd.Parameters.AddRange(param);
}
The Main form Code:
namespace M_Weight_System.Presentation_Layer
{
public partial class Main : Form
{
Bussiness_Layer.Cls_Data dta = new Bussiness_Layer.Cls_Data();
public Main()
{
InitializeComponent();
}
private void bSave_Click(object sender, EventArgs e)
{
try
{
dta.Add_Data(tId.Text, tNumber.Text, tClient.Text, tDriver.Text, Convert.ToInt32(tFirst.Text), Convert.ToInt32(tSecond.Text), Convert.ToInt32(rt2.Text), tDate1.Text,tCity.Text, tType.Text,tDate2.Text);
MessageBox.Show("Success");
bEdit.Enabled = true;
NewToolStripMenuItem.Enabled = true;
PrintToolStripMenuItem.Enabled = false;
}
catch
{
MessageBox.Show("Problem !");
}
}
private void bEdit_Click(object sender, EventArgs e)
{
try
{
dta.Update_Data(tId.Text, tNumber.Text, tClient.Text, tDriver.Text, Convert.ToInt32(tFirst.Text), Convert.ToInt32(tSecond.Text), Convert.ToInt32(rt2.Text), tDate1.Text, tCity.Text, tType.Text, tDate2.Text);
MessageBox.Show("Success");
}
catch
{
MessageBox.Show("Problem !");
}
}
}
}
you can try this command.Parameters.Add("@param", DBNull.Value);
or you can Set the defaults to NULL in the proc and you don't have to explicitly pass NULL to a proc.
CREATE PROC ..dbo.Proc ( @param1 int =NULL, @param2 varchar(40) = NULL ...)
Also, if you have to send NULL from your app you would use DBNull.Value
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With