Reputation: 5
Hello there I am trying to enter database records from window Form
design and programmed using MS visual C#
and it approved and entered database using an other Window Form
and I call the child Form
using showDialog
, I tried to make it entering to and sending thde data using string array and send it
as following
public string[] cashg1
{
get { return dt; }
set { dt = value; }
}
public void Entering(){
form2.ShowDialog();
string[] inputs = new string[10];
DateTime Date = DateTime.Today;
inputs[0] = numb.Text;
inputs[1] = comboBox1.SelectedText;
inputs[2] = owner.Text;
inputs[3] = curency.Text;
inputs[4] = curncyval.Text;
inputs[5] = Depet.Text;
inputs[6] = cridet.Text;
inputs[7] = Convert.ToString(Date);
inputs[8] = note.Text;
form2.addingdata(inputs);
cashg1 = inputs;
}
public void Entring_to database(){inputs = new string[10];
using (SqlConnection connection = new SqlConnection(connectionString))
{
DateTime Date = DateTime.Today;
try
{
SqlCommand cmd = new SqlCommand("INSERT INTO Accont(Account_ID,Account_Name,Owners,Curency,Curncytype,Depet,Credet_devet,Date,Note) VALUES ( @@AccountID, @AccountName, @Owner, @Curncy,@Curncytype,@Depet,@Cridetdevet,@Date,@Note)");
cmd.CommandType = CommandType.Text;
cmd.Connection = connection;
cmd.Parameters.AddWithValue("@@AccountID", Convert.ToInt32(inputs[0]));
if (String.IsNullOrEmpty(inputs[0]))
inputs[0] = "323";
cmd.Parameters.AddWithValue("@AccountName", Convert.ToString(inputs[1]));
if (String.IsNullOrEmpty(inputs[1]))
inputs[1] = " ";
cmd.Parameters.AddWithValue("@Owner", inputs[2]);
cmd.Parameters.AddWithValue("@Curncy", inputs[3]);
cmd.Parameters.AddWithValue("@Curncytype", inputs[4]);
cmd.Parameters.AddWithValue("@Depet", inputs[5]);
cmd.Parameters.AddWithValue("@Cridetdevet", inputs[6]);
cmd.Parameters.AddWithValue("@Date", Date);
cmd.Parameters.AddWithValue("@Note", inputs[7]);
connection.Open();//Owner
cmd.ExecuteNonQuery();
}
catch (Exception ee)
{
MessageBox.Show("Error :" + ee.Message);
}
}
}
}
I enter my data using this Form
:
when I clicked conforming button to enter the data in datagridview
After conforming the data must enter the database but It dosenot enter and gives my this error
thank you for helping
Upvotes: 0
Views: 110
Reputation: 26846
AddWithValue
will not add parameter, if value is null.
If setting AccountName to blank instead of null is intended, then you should change order of these strings in your code (i.e. you should initialize inputs1 if it is empty before adding this parameter, not after)
Your code
cmd.Parameters.AddWithValue("@AccountName", Convert.ToString(inputs[1]));
if (String.IsNullOrEmpty(inputs[1]))
inputs[1] = " ";
Should looks like
if (String.IsNullOrEmpty(inputs[1]))
inputs[1] = " ";
cmd.Parameters.AddWithValue("@AccountName", Convert.ToString(inputs[1]));
Instead, if null should be inserted in database if inputs1 is empty, you should use DBNull.Value
cmd.Parameters.AddWithValue("@AccountName", inputs[1] == null ? DBNull.Value : Convert.ToString(inputs[1]));
See msdn for reference.
Upvotes: 1