hi can someone please help me with this issue?
i want to send a null value to the database from datetimepicker but it doesnt work, it doesnt give an error but everytime i uncheck the checbox near on the datetimepicker the date still enters :( can anyone please help with this
private void BtnSave_Click(object sender, EventArgs e)
{
using (SqlConnection sqlConn = new SqlConnection("Data Source=TANYA-PC;Initial Catalog=biore1;Integrated Security=True"))
{
string sqlQuery = #"INSERT INTO cottonpurchase VALUES(#slipNo, #purchasedate, #farmercode, #farmername, #villagename, #basicprice, #weight, #totalamountbasic, #premium, #totalamountpremium, #totalamountpaid, #certstatus)";
using (SqlCommand cmd = new SqlCommand(sqlQuery, sqlConn))
{
cmd.Parameters.Add("#slipNo", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtSlipNo.Text)) ? int.Parse(TxtSlipNo.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#purchasedate", SqlDbType.DateTime).Value = Date.Text;
if (Date.Checked)
cmd.Parameters.AddWithValue("purchdate", Date);
else
cmd.Parameters.AddWithValue("purchdate", DBNull.Value);
cmd.Parameters.Add("#farmercode", SqlDbType.Int).Value = TxtFarmerCode.Text;
cmd.Parameters.Add("#farmername", SqlDbType.VarChar, 100).Value = TxtFarmerName.Text;
cmd.Parameters.Add("#villagename", SqlDbType.VarChar, 100).Value = TxtVillageName.Text;
cmd.Parameters.Add("#basicprice", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtBasicPrice.Text)) ? int.Parse(TxtBasicPrice.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#weight", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtWeight.Text)) ? int.Parse(TxtWeight.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#totalamountbasic", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountBasic.Text)) ? int.Parse(TxtTotalAmountBasic.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#premium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtPremium.Text)) ? int.Parse(TxtPremium.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#totalamountpremium", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPremium.Text)) ? int.Parse(TxtTotalAmountPremium.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#totalamountpaid", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtTotalAmountPaid.Text)) ? int.Parse(TxtTotalAmountPaid.Text) : (object)DBNull.Value;
//d.Parameters.Add("#yeildestimates", SqlDbType.Int).Value = (!String.IsNullOrEmpty(TxtYeildEstimates.Text)) ? int.Parse(TxtYeildEstimates.Text) : (object)DBNull.Value;
cmd.Parameters.Add("#certstatus", SqlDbType.VarChar, 100).Value = comboBox1.Text;
sqlConn.Open();
It doesn't look like parameter purchdate is used anywhere.
Maybe try something like this:
//cmd.Parameters.Add("#purchasedate", SqlDbType.DateTime).Value = Date.Text;
if (Date.Checked)
cmd.Parameters.AddWithValue("#purchasedate", Date);
else
cmd.Parameters.AddWithValue("#purchasedate", DBNull.Value);
Doesn't it have anything to do with the following lines?
cmd.Parameters.Add("#purchasedate", SqlDbType.DateTime).Value = Date.Text;
if (Date.Checked)
cmd.Parameters.AddWithValue("purchdate", Date);
else
cmd.Parameters.AddWithValue("purchdate", DBNull.Value);
You are setting the #purchasedate param outside the if, so it's always setted.
If this is not the problem, you should post the UI code, for us to have some more info to answer.
Related
This is the error
Exception Details: System.InvalidCastException: Object must implement IConvertible.
This is the code that I have, and I am tryin to solve this error for about 5 hours.
I don't know if it's something about the type of the values.
protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection sqlConnection = new SqlConnection(SqlDataSourceAutovehicule.ConnectionString);
sqlConnection.Open();
SqlCommand sqlCommand = new SqlCommand("INSERT INTO Autovehicul(Marca, Model, Stoc, Categorie) VALUES (" +
"#marca, #model, #stoc, #categorie)", sqlConnection);
SqlParameter pMarca = new SqlParameter("#marca", SqlDbType.VarChar, 50);
pMarca.Value = txtMarca.Text;
SqlParameter pModel = new SqlParameter("#model", SqlDbType.VarChar, 50);
pModel.Value = txtModel.Text;
SqlParameter pStoc = new SqlParameter("#stoc", SqlDbType.Int, 50);
pStoc.Value = txtStoc.Text;
SqlParameter pCategorie = new SqlParameter("#categorie", SqlDbType.Int, 50);
pCategorie.Value = DropDownList1.SelectedValue;
sqlCommand.Parameters.Add(pMarca);
sqlCommand.Parameters.Add(pModel);
sqlCommand.Parameters.Add(pStoc);
sqlCommand.Parameters.Add(pCategorie);
if (sqlCommand.ExecuteNonQuery() > 0)
{
Label1.Text = "adaugare reusita";
GridView1.DataBind();
}
else
{
Label1.Text = "adaugare nereusita";
}
}
A few things?
You don't have 50 digit long integer, so you can dump the optional ,50) for those parmamters.
Also, it looks like your combo box if not selected, is probably returning a "" (empty string). While that can be shoved into a paramter, when it evaluates, it can't convert to a 0.
So, I suggest this:
SqlParameter pCategorie = new SqlParameter("#categorie", SqlDbType.Int);
if (DropDownList1.SelectedValue == "")
pCategorie.Value = 0;
else
pCategorie.Value = DropDownList1SelectedValue;
So, you might want to check tht combo box for null, or a "", but that conversion is failing. And you not see the fail until the execute command.
And the same goes for your text boxes - if they ahve "" (no value, then you need to pass zero.
So, say like this:
using (SqlConnection conn = new SqlConnection(""))
{
using (SqlCommand cmdSQL = new SqlCommand(
"INSERT INTO Autovehicul (Marca, Model, Stoc, Categorie) " +
"VALUES (#marca, #model, #stoc, #categorie", conn))
{
conn.Open();
cmdSQL.Parameters.Add("#marca", SqlDbType.VarChar).Value = txtMarca.Text;
cmdSQL.Parameters.Add("#model", SqlDbType.VarChar).Value = txtModel.Text;
if (if txtSToc.Text == "")
txtStoc.Text = 0;
cmdSQL.Parameters.Add("#stoc", SqlDbType.Int, 50).Value = txtStoc.Text;
if (DropDrownList1.SelectedValue == "")
cmdSQL.Parameters.Add("#categorie", SqlDbType.Int).Value = 0;
else
cmdSQL.Parameters.Add("#categorie", SqlDbType.Int).Value = DropDownList1.SelectedValue;
if (cmdSQL.ExecuteNonQuery() > 0)
Hi I'm just starting to learn about C# on VS 2 days ago..so I'm really new on this, please pardon if this question is noob.
I'm trying to create a purchase form that will stored the data input to my db, and I already make the table same as the form.
so here's the code :
private void ConfirmBtn_Click(object sender, EventArgs e)
{
System.DateTime myDate = default(System.DateTime);
myDate = DateInput.Value;
SqlConnection con = new SqlConnection(#"Data Source=MY-PC\SQLEXPRESS;Initial Catalog=master;integrated security=SSPI");
SqlCommand cmd = new SqlCommand("INSERT INTO transaction_record values(#DATE,#OP,#NAME,#USERID,#GAME,#BANK,#AMOUNT)", con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#DATE", SqlDbType.DateTime).Value = DateInput.Text;
cmd.Parameters.AddWithValue("#OP", SqlDbType.VarChar).Value = OpInput.Text;
cmd.Parameters.AddWithValue("#NAME", SqlDbType.VarChar).Value = MemberInput.Text;
cmd.Parameters.AddWithValue("#USERID", SqlDbType.VarChar).Value = MemberIdInput.Text;
cmd.Parameters.AddWithValue("#GAME", SqlDbType.VarChar).Value = GameInput.Text;
cmd.Parameters.AddWithValue("#BANK", SqlDbType.VarChar).Value = BankInput.Text;
cmd.Parameters.AddWithValue("#AMOUNT", SqlDbType.Money).Value = Decimal.Parse(AmountInput.Text);
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
and my AmountInput.text is using telerik masked textbox with number masking.
When i try to store the value to my db.transaction_record #amount column that has money datatype, it return error "cannot convert 'int' to 'System.Globalization.NumberStyles'
Please help me..
I'm new to C# coding and this website so bear with me. On the button click I want my stored procedure to execute and populate the datagridview. When I run the application and click the button I keep getting an error saying:
"No mapping exists from object type System.Windows.Forms.DataGridViewTextBoxCell to a known managed provider native type."
How do I fix this? I've tried converting the parameters to their data types which doesn't seem to work either. I've been stuck on this for a while and am struggling to find anything on the internet about my specific error. Thank you.
private void button1_Click(object sender, EventArgs e)
{
SqlConnection sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
//Stored Procedure
SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1);
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
sqlcn1 = new SqlConnection("My Server Connection String");
sqlcn1.Open();
foreach (DataGridViewRow row in dataGridView1.Rows) ;
SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1);
//Stored Procedure
sqlcmdins.CommandType = CommandType.StoredProcedure;
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[0];
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[1];
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[2];
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = dataGridView1.Rows[Ournum].Cells[3];
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[4];
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[5];
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[6];
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = dataGridView1.Rows[Ournum].Cells[7];
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[8];
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = dataGridView1.Rows[Ournum].Cells[9];
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[10];
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[11];
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[12];
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[13];
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[14];
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = dataGridView1.Rows[Ournum].Cells[15];
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = dataGridView1.Rows[Ournum].Cells[16];
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = dataGridView1.Rows[Ournum].Cells[17];
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[18];
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = dataGridView1.Rows[Ournum].Cells[19];
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = dataGridView1.Rows[Ournum].Cells[20];
Your issue is that you're trying to pass the Cell into the stored procedure. You need to pass in the cells value.
Also, because you've put a ';' at the end of the foreach call, your loop around the rows isn't doing anything. Assuming you're trying to insert all the rows from the datagridview, you'll need something like this...
using (SqlConnection sqlcn1 = new SqlConnection("My Server Connection String"))
{
sqlcn1.Open();
//Stored Procedure
using (SqlCommand sqlcmddel = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmddel.CommandType = CommandType.StoredProcedure;
sqlcmddel.ExecuteNonQuery();
}
foreach (DataGridViewRow row in dataGridView1.Rows)
{
using (SqlCommand sqlcmdins = new SqlCommand("My_Stored_Procedure", sqlcn1))
{
sqlcmdins.CommandType = CommandType.StoredProcedure;
//Stored Procedure
sqlcmdins.Parameters.Add("#sugnum", SqlDbType.Int).Value = row.Cells[0].Value;
sqlcmdins.Parameters.Add("#sugtype", SqlDbType.NVarChar, 50).Value = row.Cells[1].Value;
sqlcmdins.Parameters.Add("#buyerid", SqlDbType.NVarChar, 50).Value = row.Cells[2].Value;
sqlcmdins.Parameters.Add("#duedate", SqlDbType.DateTime).Value = row.Cells[3].Value;
sqlcmdins.Parameters.Add("#xrelqty", SqlDbType.Float).Value = row.Cells[4].Value;
sqlcmdins.Parameters.Add("#purchasingfactor", SqlDbType.Float).Value = row.Cells[5].Value;
sqlcmdins.Parameters.Add("#relqty", SqlDbType.Float).Value = row.Cells[6].Value;
sqlcmdins.Parameters.Add("#jobnum", SqlDbType.NVarChar, 20).Value = row.Cells[7].Value;
sqlcmdins.Parameters.Add("#assemblyseq", SqlDbType.SmallInt).Value = row.Cells[8].Value;
sqlcmdins.Parameters.Add("#jobseq", SqlDbType.SmallInt).Value = row.Cells[9].Value;
sqlcmdins.Parameters.Add("#warehousecode", SqlDbType.NVarChar, 50).Value = row.Cells[10].Value;
sqlcmdins.Parameters.Add("#fob", SqlDbType.NVarChar, 50).Value = row.Cells[11].Value;
sqlcmdins.Parameters.Add("#shipviacode", SqlDbType.NVarChar, 50).Value = row.Cells[12].Value;
sqlcmdins.Parameters.Add("#termscode", SqlDbType.NVarChar, 50).Value = row.Cells[13].Value;
sqlcmdins.Parameters.Add("#vendornum", SqlDbType.Int).Value = row.Cells[14].Value;
sqlcmdins.Parameters.Add("#purpoint", SqlDbType.Int).Value = row.Cells[15].Value;
sqlcmdins.Parameters.Add("#linedesc", SqlDbType.NVarChar, 50).Value = row.Cells[16].Value;
sqlcmdins.Parameters.Add("#ium", SqlDbType.NVarChar, 10).Value = row.Cells[17].Value;
sqlcmdins.Parameters.Add("#unitprice", SqlDbType.Float).Value = row.Cells[18].Value;
sqlcmdins.Parameters.Add("#docunitprice", SqlDbType.Float).Value = row.Cells[19].Value;
sqlcmdins.Parameters.Add("#taxable", SqlDbType.Bit).Value = row.Cells[20].Value;
sqlcmdins.ExecuteNonQuery();
}
}
}
I am trying to insert empty text box value into database by stored procedure.But i do
not know how to pass null values through stored procedure please help me. My Class is
public Void empqualadd(string id, string name, string qual1)
{
SqlCommand cmd = new SqlCommand("InsertQual");
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("#MachID", SqlDbType.Int).Value = id;
cmd.Parameters.Add("#EmpCode", SqlDbType.VarChar).Value = name;
cmd.Parameters.Add("#Qualification1", SqlDbType.VarChar).Value = qual1;
conn.nonquery(cmd);
}
My Button Click is,
protected void Button1_Click(object sender, EventArgs e)
{
mas.empqualadd(ddis.SelectedItem.Text,
txtfname.Text,ddqual.SelectedItem.Text);
}
I have a connection clas too,
public Connection()
{
conn = new SqlConnection(#"server=SIGNET- SOFTWARE\SA;database=manjilas;Integrated security=true");
cmd = null;
}
public void nonquery(SqlCommand cmd)//for insert,delete,update
{
if (conn.State == ConnectionState.Open)
{
conn.Close();
}
conn.Open();
cmd.Connection = conn;
cmd.ExecuteNonQuery();
conn.Close();
}
It works fine for insert data if text box is not empty.What changes I should made in class to pass null values?
Please Help me.Thanks in advance....
Try this
cmd.Parameters.Add("#MachID", SqlDbType.Int).Value = string.IsNullOrEmpty(id) ? (object)DbNull.Value : id;
Also why are you passing string value when column data type is int?
This also works fine.
if(!string.IsNullOrWhiteSpace(qual2)&!string.IsNullOrWhiteSpace(clg2)&!string.IsNullOrWhiteSpace(mark2)&!string.IsNullOrWhiteSpace(year2))
{
cmd.Parameters.Add("#Qualification2", SqlDbType.VarChar).Value = qual2.Length > 0 ? qual2 : (object)DBNull.Value;
cmd.Parameters.Add("#College2", SqlDbType.VarChar).Value = clg2.Length > 0 ? clg2 : (object)DBNull.Value;
cmd.Parameters.Add("#Mark2", SqlDbType.Int).Value = mark2.Length > 0 ? mark2 : (object)DBNull.Value;
cmd.Parameters.Add("#Year2", SqlDbType.VarChar).Value =year2.Length > 0 ? year2 : (object)DBNull.Value;
conn.nonquery(cmd);
}
I'm going crazy here, can someone please tell me what's wrong with this code
con.Open();
cmd = new SqlCommand("INSERT INTO COUNTERS(COUNTER_START, COUNTER_CURRENT, COUNTER_NEXT, COUNTER_TYPE, COUNTER_DATE_CREATED, COUNTER_TIME_CREATED, COUNTER_CREATED_BY) " +
"VALUES(#counter_start, #counter_current, #counter_next, #counter_type, #date, #time, #user)", con);
cmd.Parameters.Add("#counter_start", SqlDbType.Int).Value = counter_start.Text;
cmd.Parameters.Add("#counter_current", SqlDbType.Int).Value = counter_current.Text;
cmd.Parameters.Add("#counter_next", SqlDbType.Int).Value = counter_next.Text;
cmd.Parameters.Add("#counter_type", SqlDbType.VarChar, 10).Value = counter_type.Text;
cmd.Parameters.Add("#date", SqlDbType.VarChar, 20).Value = date;
cmd.Parameters.Add("#time", SqlDbType.VarChar, 20).Value = time;
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user;
cmd.ExecuteNonQuery();
cmd.Dispose();
con.Close();
I'm getting this error
The parameterized query '(#counter_start int,#counter_current int,#counter_next int,#coun' expects the parameter '#user', which was
not supplied
Any input will be really appreciate it.
You may try debugging the application and see what value is being held by variable user. I think the user you are supplying is null. If you are using C# 4.0 then try
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user ?? DBNull.Value;
or for earlier versions of C#
cmd.Parameters.Add("#user", SqlDbType.VarChar, 50).Value = user != null ? user : DBNull.Value;
You may also use string.Empty in place of DBNull.Value, if you want an empty string instead of DBNull