I'm trying to insert something into a SQL server (using c#) and when I try to it says it requires the (in the query) given parameter and that it can't find it while it is declared.
"Additional information: The parameterized query '(#art varchar(8000),#oms varchar(8000),#rem varchar(8000),#artdk' expects the parameter '#art', which was not supplied."
I checked parameter amount, the database connection and I tried to use another insert command I have as reference but I couldn't get it to work. and I couldn't find the same sort of problem on the internet yet. if someone could help me, it would be much appreciated.
UPDATE:
I changed the artnr.selectedvalue to artnr.text and that sort of got me some progress but now I get this error
UPDATE2:
seems that if I fill every box it tries to insert it works.. yet it does allow null in all database cell's so the issue has been found. just got to work out which one('s) cant have empty.
An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll
Additional information: Error converting data type varchar to numeric.
it gives this error when I put in all numeric or all text
SqlCommand slinkoopadd = new SqlCommand(#"insert into ART (ART ,OMS ,REM ,ARTDK ,TYPE ,MAG ,PROGRAM ,EH1 ,LEV ,LTD ,INK ,KOR ,SGR ,EH2 ,EF ,VALUTA ,CRNI )
values (#art,#oms,#rem,#artdk,#type,#mag,#program,#eh1,#lev,#ltd,#ink,#kor,#sgr,#eh2,#ef,#valuta,#crni);", Connectie.connMEVO_ART);
#region parameters
slinkoopadd.Parameters.Add("#art", SqlDbType.VarChar).Value = this.artnr.SelectedValue;
slinkoopadd.Parameters.Add("#oms", SqlDbType.VarChar).Value = this.tekstinkoopoms.Text;
slinkoopadd.Parameters.Add("#rem", SqlDbType.VarChar).Value = this.tekstinkoopopmerk.Text;
slinkoopadd.Parameters.Add("#artdk", SqlDbType.VarChar).Value = this.tekstinkoopnummerlev.Text;
slinkoopadd.Parameters.Add("#type", SqlDbType.VarChar).Value = this.tekstinkooparttype.Text;
slinkoopadd.Parameters.Add("#mag", SqlDbType.VarChar).Value = this.tekstinkoopmagazijnloc.Text;
slinkoopadd.Parameters.Add("#program", SqlDbType.VarChar).Value = this.tekstinkoopinternopmerk.Text;
slinkoopadd.Parameters.Add("#eh1", SqlDbType.VarChar).Value = this.tekstinkoopeenheid.Text;
slinkoopadd.Parameters.Add("#lev", SqlDbType.VarChar).Value = this.tekstinkoopstandleveran.Text;
slinkoopadd.Parameters.Add("#ltd", SqlDbType.VarChar).Value = this.tekstinkooplevertijd.Text;
slinkoopadd.Parameters.Add("#ink", SqlDbType.VarChar).Value = this.tekstinkoopbrutoprijs.Text;
slinkoopadd.Parameters.Add("#kor", SqlDbType.VarChar).Value = this.tekstinkoopkorting.Text;
slinkoopadd.Parameters.Add("#sgr", SqlDbType.VarChar).Value = this.tekstinkoopserievoorraad.Text;
slinkoopadd.Parameters.Add("#eh2", SqlDbType.VarChar).Value = this.tekstinkoopgebruikeh.Text;
slinkoopadd.Parameters.Add("#ef", SqlDbType.VarChar).Value = this.textinkoopehfactor.Text;
//slinkoopadd.Parameters.Add("#", SqlDbType.VarChar).Value = this.artnr.Text;//perc. voor vracht
slinkoopadd.Parameters.Add("#valuta", SqlDbType.VarChar).Value = this.tekstinkoopvaluta.Text;
slinkoopadd.Parameters.Add("#crni", SqlDbType.VarChar).Value = this.tekstinkoopcrni.Text;
//slinkoopadd.Parameters.Add("#", SqlDbType.VarChar).Value = this.artnr.Text;//extra kosten
//slinkoopadd.Parameters.Add("#", SqlDbType.VarChar).Value = this.artnr.Text;//bestelgrootte afroep
//slinkoopadd.Parameters.Add("#", SqlDbType.VarChar).Value = this.artnr.Text;//prognose jaarverbruik
//slinkoopadd.Parameters.Add("#", SqlDbType.VarChar).Value = this.artnr.Text;//levertijd nieuwe afr
#endregion
drART = slinkoopadd.ExecuteReader();
MessageBox.Show("Opgeslagen!");
fillbox();
while (drART.Read())
{ }
slinkoopadd.Dispose();
Try it like this?
slinkoopadd.Parameters.Add("#art", SqlDbType.VarChar);
slinkoopadd.Parameters["#art"].Value = this.artnr.SelectedValue;
I'm not sure you can pipe the call like that.
drART = slinkoopadd.ExecuteReader();
You don't expect to read something. You want to insert, that is not a query:
var result = slinkoopadd.ExecuteNonQuery();
i changed some small things in the query and i made sure some of the textboxes get a default value if it doesnt have anything in it so now it works. this is the current query that works.
if (tekstinkoopcrni.Text == "1") { } else if (tekstinkoopcrni.Text == "0") { }
else { tekstinkoopcrni.Text = "0"; }
if (tekstinkoopkorting.Text == "") { tekstinkoopkorting.Text = "0"; }
if (tekstinkoopserievoorraad.Text == "") { tekstinkoopserievoorraad.Text = "0"; }
if (tekstinkoopstandleveran.Text == "") { tekstinkoopstandleveran.Text = "9999"; }
if (tekstinkooplevertijd.Text == "") { tekstinkooplevertijd.Text = "0"; }
SqlCommand slinkoopadd = new SqlCommand(#"insert into ART (ART ,OMS ,REM ,ARTDK ,TYPE ,
MAG ,PROGRAM ,EH1 ,INK ,KOR ,SGR ,EH2 ,EF ,VALUTA ,CRNI ,LEV ,LTD )
values (#art,#oms,#rem,#artdk,#type,#mag,#program,#eh1,#ink,#kor,#sgr,#eh2,#ef,
#valuta,#crni, #lev,#ltd);", Connectie.connMEVO_ART);
#region parameters
slinkoopadd.Parameters.Add("#art", SqlDbType.VarChar).Value = this.artnr.Text;
slinkoopadd.Parameters.Add("#oms", SqlDbType.VarChar).Value = this.tekstinkoopoms.Text;
slinkoopadd.Parameters.Add("#rem", SqlDbType.VarChar).Value = this.tekstinkoopopmerk.Text;
slinkoopadd.Parameters.Add("#artdk", SqlDbType.VarChar).Value = this.tekstinkoopnummerlev.Text;
slinkoopadd.Parameters.Add("#type", SqlDbType.VarChar).Value = this.tekstinkooparttype.Text;
slinkoopadd.Parameters.Add("#mag", SqlDbType.VarChar).Value = this.tekstinkoopmagazijnloc.Text;
slinkoopadd.Parameters.Add("#program", SqlDbType.VarChar).Value = this.tekstinkoopinternopmerk.Text;
slinkoopadd.Parameters.Add("#eh1", SqlDbType.VarChar).Value = this.tekstinkoopeenheid.Text;
slinkoopadd.Parameters.Add("#lev", SqlDbType.VarChar).Value = this.tekstinkoopstandleveran.Text;
slinkoopadd.Parameters.Add("#ltd", SqlDbType.VarChar).Value = this.tekstinkooplevertijd.Text;
slinkoopadd.Parameters.Add("#ink", SqlDbType.VarChar).Value = this.tekstinkoopbrutoprijs.Text;
slinkoopadd.Parameters.Add("#kor", SqlDbType.VarChar).Value = this.tekstinkoopkorting.Text;
slinkoopadd.Parameters.Add("#sgr", SqlDbType.VarChar).Value = this.tekstinkoopserievoorraad.Text;
slinkoopadd.Parameters.Add("#eh2", SqlDbType.VarChar).Value = this.tekstinkoopgebruikeh.Text;
slinkoopadd.Parameters.Add("#ef", SqlDbType.VarChar).Value = this.textinkoopehfactor.Text;
slinkoopadd.Parameters.Add("#valuta", SqlDbType.VarChar).Value = this.tekstinkoopvaluta.Text;
slinkoopadd.Parameters.Add("#crni", SqlDbType.VarChar).Value = this.tekstinkoopcrni.Text;
#endregion
drART = slinkoopadd.ExecuteReader();
MessageBox.Show("Artikel opgeslagen!");
fillbox();
while (drART.Read())
{ }
slinkoopadd.Dispose();
Related
it always show an error Incorrect syntax near ')'.
I didnt see any wrong inputs
See my code below
byte[] content = ImageToStream(fName);
cnn.Open();
string sql = "update tblbarangayofficials set pic=#pic,fname=#fname,mname=#mname,lname=#lname,position=#position,startterm=#startterm,endterm=#endterm where id=#id)";
SqlCommand cmd1 = new SqlCommand(sql, cnn);
cmd1.Parameters.AddWithValue("#pic", SqlDbType.Image).Value = content;
cmd1.Parameters.AddWithValue("#fname", SqlDbType.VarChar).Value = txtfirstname.Text;
cmd1.Parameters.AddWithValue("#mname", SqlDbType.VarChar).Value = textBox1.Text;
cmd1.Parameters.AddWithValue("#lname", SqlDbType.VarChar).Value = txtlastname.Text;
cmd1.Parameters.AddWithValue("#position", SqlDbType.VarChar).Value = comboBox2.Text;
cmd1.Parameters.AddWithValue("#startterm", SqlDbType.DateTime).Value = dateTimePicker2.Value.Date;
cmd1.Parameters.AddWithValue("#endterm", SqlDbType.DateTime).Value = dateTimePicker1.Value.Date;
cmd1.Parameters.AddWithValue("#id", SqlDbType.Int).Value = int.Parse(ID.Text);
cmd1.ExecuteNonQuery();
cnn.Close();
MessageBox.Show("successfully updated");
dataGridView1.DataSource = db.sp_viewofficials();
it should save to sql server my save works
Your update statement has extra ending bracket which is not needed.
"update tblbarangayofficials set pic=#pic,fname=#fname,mname=#mname,lname=#lname,position=#position,startterm=#startterm,endterm=#endterm where id=#id"
using (SqlCommand sc = new SqlCommand("Purchase_Order_History", con))
{
MessageBox.Show("Agya");
sc.CommandType = CommandType.StoredProcedure;
sc.Parameters.Add("#Invoice_no", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column1"].Value);
sc.Parameters.Add("#Date_of_Purchase", SqlDbType.Date).Value = Date_of_Purchase.Value.ToString("yyyy/MM/dd");
sc.Parameters.Add("#Item_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column2"].Value;
sc.Parameters.Add("#Seller_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column3"].Value;
sc.Parameters.Add("#Company_Name", SqlDbType.NVarChar).Value = dgv.Rows[i].Cells["Column4"].Value;
sc.Parameters.Add("#Quantity", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column5"].Value);
sc.Parameters.Add("#Unit_Price", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column6"].Value);
sc.Parameters.Add("#Total_Price", SqlDbType.Int).Value = Convert.ToInt32(dgv.Rows[i].Cells["Column7"].Value);
sc.Parameters.Add("#Discount", SqlDbType.Int).Value = Convert.ToInt32(Discount.Text);
sc.Parameters.Add("#Paid_Amount", SqlDbType.Int).Value = Convert.ToInt32(Paid_Amount.Text);
sc.Parameters.Add("#Remaining", SqlDbType.Int).Value = Convert.ToInt32(lbl_Remaining.Text);
sc.Parameters.Add("#Sub_Total", SqlDbType.Int).Value = Convert.ToInt32(lbl_Subtotal.Text);
sc.Parameters.Add("#Total", SqlDbType.Int).Value = Convert.ToInt32(lbl_Total.Text);
con.Open();
sc.ExecuteNonQuery();
con.Close();
}
I don't know what is happening here I am getting this exception Input String Was Not In a Correct Format
You are converting the date to a string with Date_of_Purchase.Value.ToString("yyyy/MM/dd"). But the parameter #Date_of_Purchase is of type SqlDbType.Date, not of a text type. Therefore, change the code line to
sc.Parameters.Add("#Date_of_Purchase", SqlDbType.Date).Value = Date_of_Purchase.Value;
(Assuming that Date_of_Purchase is a DateTime? and you checked that it is not null.)
Note: You cannot format a date when storing it in a date or date/time column. You have to format it when displaying it.
This is basically a method to insert a record into a table. It was working fine before I decided to add in a way to check if the Customer ID already exists in the database. I get a
'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code
Additional information: Procedure or function InsertCustomer has too many arguments specified.
on the line
command.ExecuteNonQuery();
I don't understand what's wrong.
public void add()
{
lblMessage.Text = "";
command.Connection = conn;
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "CheckDetails";
command.Parameters.AddWithValue("#CustID", txtCID.Text);
conn.Open();
int check = (int)command.ExecuteScalar();
if (check == 0)
{
command.CommandText = "InsertCustomer";
command.Parameters.Add("#CustID", SqlDbType.Int).Value = txtCID.Text;
command.Parameters.Add("#FirstName", SqlDbType.VarChar).Value = txtFName.Text;
command.Parameters.Add("#Surname", SqlDbType.VarChar).Value = txtLName.Text;
command.Parameters.Add("#Gender", SqlDbType.VarChar).Value = rdoGender.Text;
command.Parameters.Add("#Age", SqlDbType.Int).Value = txtAge.Text;
command.Parameters.Add("#Address1", SqlDbType.VarChar).Value = txtAdd1.Text;
command.Parameters.Add("#Address2", SqlDbType.VarChar).Value = txtAdd2.Text;
command.Parameters.Add("#City", SqlDbType.VarChar).Value = txtCity.Text;
command.Parameters.Add("#Phone", SqlDbType.VarChar).Value = txtPhone.Text;
command.Parameters.Add("#Mobile", SqlDbType.VarChar).Value = txtMobile.Text;
command.Parameters.Add("#Email", SqlDbType.VarChar).Value = txtEmail.Text;
command.ExecuteNonQuery();
lblMessage.Text = "Customer Details Added.";
}
else
{
lblMessage.Text = "Customer ID already exists.";
}
conn.Close();
}
You are adding the same parameter twice:
command.Parameters.AddWithValue("#CustID", txtCID.Text);
// ....
command.Parameters.Add("#CustID", SqlDbType.Int).Value = txtCID.Text;
You can use command.Parameters.Clear();. But i'd prefer to use two different SqlCommands for the two procedures CheckDetails and InsertCustomer to avoid such issues.
Side-note: don't let the database try-cast the value for you. Use int.TryParse.
Remove below parameter from your statement, you already add parameter in command:
command.Parameters.Add("#CustID", SqlDbType.Int).Value = txtCID.Text;
Receiving this error when I try saving.
Incorrect Syntax near the word update.
Seems like an obvious fix but I can't seem to find it. Hoping fresh eyes will help! Thanks
protected void btnSave_Click(object sender, EventArgs e)
{
Button EditButton = (Button)EditLoginView.FindControl("EditButton");
Button SaveButton = (Button)EditLoginView.FindControl("SaveButton");
TitleLanguage.ActiveViewIndex = 0;
LanguageView.ActiveViewIndex = 0;
EditButton.Visible = true;
SaveButton.Visible = false;
//update the file in the database
string strQuery = "UPDATE pages SET en_content = #en_Content, fr_Content = #fr_content, fr_Title=#fr_title, en_Title=#en_title, update=#update WHERE link_title = #link_title";
SqlCommand cmd = new SqlCommand(strQuery);
cmd.Parameters.Add("#en_title", SqlDbType.VarChar).Value = Edit_EnglishT.Text;
cmd.Parameters.Add("#fr_title", SqlDbType.VarChar).Value = Edit_FrenchT.Text;
cmd.Parameters.Add("#en_content", SqlDbType.VarChar).Value = Edit_English.Text;
cmd.Parameters.Add("#fr_content", SqlDbType.VarChar).Value = Edit_French.Text;
cmd.Parameters.Add("#update", SqlDbType.DateTime).Value = DateTime.Now;
cmd.Parameters.Add("#link_", SqlDbType.VarChar).Value = linktitle;
UpdateData(cmd);
}
private Boolean UpdateData(SqlCommand cmd)
{
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["randolphConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(strConnString);
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
try
{
con.Open();
cmd.ExecuteNonQuery();
return true;
}
catch (Exception ex)
{
Response.Write(ex.Message);
return false;
}
finally
{
con.Close();
con.Dispose();
}
}
UPDATE is a reserved keyword in T-SQL. You should use it with square brackets like [UPDATE]
Like;
string strQuery = #"UPDATE pages SET en_content = #en_Content, fr_Content = #fr_content, fr_Title=#fr_title, en_Title=#en_title,
[update]=#update WHERE link_title = #link_title";
^^^^^^^^
As a general recomendation, don't use reserved keywords for your identifiers and object names in your database.
Also change your
cmd.Parameters.Add("#link_", SqlDbType.VarChar).Value = linktitle;
to
cmd.Parameters.Add("#link_title", SqlDbType.VarChar).Value = linktitle;
Because you declared your parameter name as #link_title not #link_ in your strQuery.
EDIT: For clarification, you don't need to use a method (UpdateData) for that such a process. Just use like this;
String strConnString = System.Configuration.ConfigurationManager.ConnectionStrings["randolphConnectionString"].ConnectionString;
using(SqlConnection con = new SqlConnection(strConnString))
using(SqlCommand cmd = con.CreateCommand())
{
string strQuery = #"UPDATE pages SET en_content = #en_Content, fr_Content = #fr_content, fr_Title=#fr_title, en_Title=#en_title, [update]=#update WHERE link_title = #link_title";
cmd.CommandText = strQuery;
cmd.Parameters...;
.....
.....
cmd.Connection.Open();
cmd.ExecuteNonQuery();
}
You define the field update which is a reserved keyword.
Try [update] instead like in this sample:
UPDATE pages
SET en_content = #en_Content
, fr_Content = #fr_content
, fr_Title=#fr_title
, en_Title=#en_title
, [update]=#update
WHERE link_title = #link_title
In your last parameter, you use #link_title
but in the Parameter.Add, you use:
cmd.Parameters.Add("#link_", SqlDbType.VarChar).Value = linktitle;
Try to change for this one:
cmd.Parameters.Add("#link_title", SqlDbType.VarChar).Value = linktitle;
Hi i want to update my ITEM_DETAILS table in C# using Sql server at the backend but when i am doing this i am getting the following error :
failed to convert parameter value from a string to a decimal. c#
Here is my code:
int x;
da.UpdateCommand = new SqlCommand("UPDATE ITEM_DETAILS SET ITEM_NAME=#ITEM_NAME,ITEM_DESCRIPTION=#ITEM_DESCRIPTION,VENDOR_NAME=#VENDOR_NAME,QUANTITY=#QUANTITY,RATE=#RATE,AMOUNT=#AMOUNT,INVOICE_NUM=#INVOICE_NUM,DATE=#DATE WHERE ITEM_MODEL=#ITEM_MODEL", con);
da.UpdateCommand.Parameters.Add("#ITEM_NAME", SqlDbType.VarChar).Value = txtItemName.Text;
da.UpdateCommand.Parameters.Add("#ITEM_DESCRIPTION", SqlDbType.VarChar).Value = txtItemDescription.Text;
da.UpdateCommand.Parameters.Add("#VENDOR_NAME", SqlDbType.VarChar).Value = txtVendor.Text;
da.UpdateCommand.Parameters.Add("#QUANTITY", SqlDbType.Int).Value = txtQuantity.Text;
da.UpdateCommand.Parameters.Add("#RATE", SqlDbType.Money).Value = txtRate.Text;
da.UpdateCommand.Parameters.Add("#AMOUNT", SqlDbType.Money).Value = txtAmount.Text;
da.UpdateCommand.Parameters.Add("#INVOICE_NUM", SqlDbType.Int).Value = txtInvoice.Text;
da.UpdateCommand.Parameters.Add("#DATE", SqlDbType.VarChar).Value = dateTimePicker1.Value;
da.UpdateCommand.Parameters.Add("#ITEM_MODEL", SqlDbType.VarChar).Value = ds.Tables[0].Rows[bs.Position][0];
con.Open();
x = da.UpdateCommand.ExecuteNonQuery();
con.Close();
if (x >= 1)
{
MessageBox.Show("Record(s) has been updated");
}
Can anyone explain me the solution to this problem?
da.UpdateCommand.Parameters.Add("#RATE", SqlDbType.Money).Value = txtRate.Text;
da.UpdateCommand.Parameters.Add("#AMOUNT", SqlDbType.Money).Value = txtAmount.Text;
You need to parse the txtRate.Text and txtAmount.Text to Currency.
string.Format("{0:#.00}", Convert.ToDecimal(myMoneyString) / 100);
See Convert String to Money
In VB, we would use the CType() conversion.