I'm trying to insert new values into my database using visual studio and here is my code:
SqlCommand cmd = new SqlCommand();
SqlConnection conn = new SqlConnection();
conn.ConnectionString = dbconnectionstring;
conn.Open();
cmd.Connection = conn;
cmd.CommandText = "INSERT INTO Data_ConfigInfo (TypeID, Name, ValidFromDate, ValidToDate, ValidFromSOP, ValidToSOP, Description)" +
"VALUES(#TypeID, #Name, #ValidFromDateTime, #ValidToDateTime, #ValidFromSOP, #ValidToSOP, #Description)";
cmd.Parameters.AddWithValue("TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("Name", Logg.Name);
if(Logg.ValidFrom.Equals(null)) {
cmd.Parameters.AddWithValue("#ValidFromDateTime", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("#ValidFromDateTime", Logg.ValidFrom);
}
if (Logg.ValidTo.Equals(null))
{
cmd.Parameters.AddWithValue("#ValidToDateTime", DBNull.Value);
}
else
{
cmd.Parameters.AddWithValue("#ValidToDateTime", Logg.ValidFrom);
}
cmd.Parameters.AddWithValue("#ValidFromSOP", Logg.ValidFromSOP);
cmd.Parameters.AddWithValue("#ValidToSOP", Logg.ValidToSOP);
cmd.Parameters.AddWithValue("#Description", Logg.Description);
cmd.ExecuteNonQuery();
conn.close();
conn.dispose();
But at the line
cmd.ExecuteNonQuery();
I get the error
invalid column name for ValidToDateTime and ValidFromDateTime.
Both are datetime variables. I can't seem to find the error. Any ideas?
This is how my datatable looks
Your DB table has columns ValidFromDateTime, ValidToDateTime, but your column list in the INSERT statement has ValidFromDate, ValidToDate. Are you sure that it is not a typo when posting here?
Did you forgot '#'?
cmd.Parameters.AddWithValue("TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("Name", Logg.Name);
cmd.Parameters.AddWithValue("#TypeID", Logg.TypeID);
cmd.Parameters.AddWithValue("#Name", Logg.Name);
Related
Context: I'm developing an app for windows in Visual Studio that has a table of stock materials and another of buyed materials, both in a Sql Server.
I want that every time you buy something it is added into the stock table.
I'm new in using SQL with c# combined.
I'm trying this from a tutorial, but does nothing. Not even an exception.
string cmdString = "Insert INTO Table1 (Column_name) VALUES (#val1)";
string connString = #"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True";
using (SqlConnection conn = new SqlConnection(connString))
{
using (SqlCommand comm = conn.CreateCommand())
{
comm.Connection = conn;
comm.CommandText = cmdString;
comm.Parameters.AddWithValue("#val1", Value);
try
{
conn.Open();
comm.ExecuteNonQuery();
conn.Close()
}
catch(SqlException ex)
{
}
}
}
Is this totally wrong or should i change something?
Edit: I figured out. I was inserting val1 in a column, but the ID was empty so it throws an NullId exception. For some reason in debug mode I wasn't able to see it.
Thanks for the help. If I have the Table1 with autoincrement why it needs an ID? There is a way that when something is inserted the Id generates automatically?
You can use this query to insert data like that :
{
if (con.State == ConnectionState.Open)
con.Close();
}
{
SqlCommand cmd0561 = new SqlCommand(#"insert into Table1 (value1,value1) values
(#value1,#timee)", con);
cmd0561.Parameters.AddWithValue("#value1", value1.Text.Trim);
cmd0561.Parameters.AddWithValue("#value2", value2.Text.Trim);
con.Open();
cmd0561.ExecuteNonQuery();
con.Close();
}
I am getting continuously an error on ExecuteNonQuery();. I have tried many ways but don't know where I am wrong.
I am using float data type in orderid, price, quantity, discount, order price columns in SQL Server.
Kindly help.
string query = "Insert into dbo.orders (OrderType,Product_Name,Product_Category,Product_Quantity,Product_Price,Date,Discount) values(#txt_rdvalue,#cb_oname,#cb_ocat,#cb_oqty,#txt_oprice,#Date,#txt_disc)";
if (string.IsNullOrWhiteSpace(txt_rdvalue.Text) || string.IsNullOrWhiteSpace(cb_oname.Text) || string.IsNullOrWhiteSpace(cb_ocat.Text) || string.IsNullOrWhiteSpace(cb_oqty.Text))
{
lbl_incorrect.Text = "please fill up all the fields";
lbl_incorrect.Visible = true;
}
else
{
using (SqlConnection con = new SqlConnection(#"Data Source=.;Initial Catalog=Pizza Mania;Integrated Security=True"))
{
using (SqlCommand cmd = new SqlCommand(query, con))
{
cmd.CommandType = CommandType.Text;
cmd.Connection = con;
con.Open();
cmd.Parameters.AddWithValue("#txt_rdvalue",txt_rdvalue.Text);
cmd.Parameters.AddWithValue("#txt_orderid",txt_orderid.Text);
cmd.Parameters.AddWithValue("#cb_oname", cb_oname.SelectedText);
cmd.Parameters.AddWithValue("#cb_ocat", cb_ocat.SelectedText);
cmd.Parameters.AddWithValue("#cb_oqty", cb_oqty.SelectedValue);
cmd.Parameters.AddWithValue("#txt_oprice", (txt_oprice.Text));
cmd.Parameters.AddWithValue("#txt_disc", txt_rdvalue.Text);
cmd.Parameters.Add(new SqlParameter("#Date", dateTimePicker1.Value.Date));
cmd.ExecuteNonQuery();
MessageBox.Show("Data Inserted");
}
}
}
The error message I am getting:
The parameterized query '(#txt_rdvalue nvarchar(8),#txt_orderid
int,#cb_oname nvarchar(40' expects the parameter '#cb_oqty', which was
not supplied
yes i have done it. The convert to double thing works
thankyou all
Appriciated.[enter link description here][1]
cmd.Parameters.AddWithValue("#txt_rdvalue",txt_rdvalue.Text);
cmd.Parameters.AddWithValue("#txt_orderid",Convert.ToDouble(txt_orderid.Text));
cmd.Parameters.AddWithValue("#cb_oname", cb_oname.SelectedText);
cmd.Parameters.AddWithValue("#cb_ocat", cb_ocat.SelectedText);
cmd.Parameters.AddWithValue("#cb_oqty", Convert.ToDouble(cb_oqty.SelectedValue));
cmd.Parameters.AddWithValue("#txt_oprice",Convert.ToDouble((txt_oprice.Text)));
cmd.Parameters.AddWithValue("#txt_disc",Convert.ToDouble(txt_disc.Text));
cmd.Parameters.Add(new SqlParameter("#Date", dateTimePicker1.Value.Date));
[1]: http://www.stackoverflow.com/alygorejaanswers
You are providing #txt_orderid(cmd.Parameters.AddWithValue("#txt_orderid",txt_orderid.Text);) parameter which doesn't exists.Check your code once again.
If it's autoincrement coloumn why are you providing this.
Also datatype mismatching is happening.
I have a database that contains a Customer table with the following columns : CustID, CustName, ICNumber, ContactNumber and Address. It is a service-based database.
string localdb = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
SqlConnection con = new SqlConnection(localdb);
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Customer(CustID,CustName,ICNumber,ContactNumber,Address)values(#CustID,#CustName,#ICNumber,#ContactNumber,#Address)", con);
cmd.Parameters.AddWithValue("#CustID", txtCustID.Text);
cmd.Parameters.AddWithValue("#CustName", txtCustName.Text);
cmd.Parameters.AddWithValue("#ICNumber", txtICNum.Text);
cmd.Parameters.AddWithValue("#ContactNumber", txtContact.Text);
cmd.Parameters.AddWithValue("#Address", txtAddress.Text);
cmd.ExecuteNonQuery();
con.Close();
The code compiles and runs. The problem I am having is that the record is not added into the table after cmd.ExecuteNonQuery(); is called.
Why is the record not showing up in the database table?
You forgot to close the quotes " on the insert command. It is nice to use try/catch to avoid problems with your insert, for sample:
string localdb = ConfigurationManager.ConnectionStrings["DatabaseConnectionString"].ConnectionString;
try
{
SqlConnection con = new SqlConnection(localdb))
con.Open();
SqlCommand cmd = new SqlCommand("INSERT INTO Customer(CustID, CustName, ICNumber, ContactNumber, Address) VALUES (#CustID, #CustName, #ICNumber, #ContactNumber, #Address)", con);
cmd.Parameters.AddWithValue("#CustID", txtCustID.Text);
cmd.Parameters.AddWithValue("#CustName", txtCustName.Text);
cmd.Parameters.AddWithValue("#ICNumber", txtICNum.Text);
cmd.Parameters.AddWithValue("#ContactNumber", txtContact.Text);
cmd.Parameters.AddWithValue("#Address", txtAddress.Text);
cmd.ExecuteNonQuery();
}
catch
{
// some errors
}
finally
{
if (con.State == ConnectionState.Open)
con.Close();
}
I want to update some columns on my table but ExecuteNonQuery doesn't respond (Timeout). Did I do something wrong?
Notes: in the database table, id is integer, F1 varchar2 and the parameters I am sending are string and int.
try {
using (OracleConnection con = new OracleConnection(ConString)) {
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE DB.Table "+
"SET F1= :yd" +
"WHERE ID = :id";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("yd", yd);
cmd.Parameters.Add("id", id);
cmd.ExecuteNonQuery();
con.Close();
return true;
}
}
catch (Exception ex) {
return false;
}
Thanks
It can be solved by committing or roll backing pending transactions on your Oracle developer or any other IDEs running on your machine.
You're mixing up your parameter names. There is no parameter named "F1" in your query, use "yd".
cmd.CommandText = "UPDATE DB.Table "+
"SET F1= :yd" +
"WHERE ID = :id";
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("yd", yd);
cmd.Parameters.Add("id", id);
I found that is because of other program like toad locks the query. After commiting all thing in toad, everything is solved.
Thanks for all for helping. I love you guys, i like brainstorming :)
cmd.Connection = con;
string qry = "UPDATE DB.Table "+"SET F1= #yd" +"WHERE ID = #id";
OracleCommand cmd = new OracleCommand(qry,con);
cmd.Parameters.AddWithValue("#yd", yd);
cmd.Parameters.AddWithValue("#id", id);
cmd.ExecuteNonQuery();
Is it possible to insert xml data in to an xmltype field?
I am using the following code but is throwing an error
ORA-01461: can bind a LONG value only for insert into a LONG column
.
I do not want to use ODP.NET. Can somebody give any suggestion?
OleDbConnection con = new OleDbConnection(ConfigurationManager.ConnectionStrings["OracleVAT"].ConnectionString);
try
{
string query = "update c_xml set DATA_XML = xmltype(?) where id=?";
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = con;
cmd.CommandText = query;
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("#DATA_XML", DATAXML.OuterXml);
cmd.Parameters.AddWithValue("#id", ID);
con.Open();
return cmd.ExecuteNonQuery();
}
catch
{
}
finally
{
con.Close();
}
You can also use the standard java api:
Something like this:
string query = "update c_xml set DATA_XML = xmltype(?) .....
Clob clob = conn.createClob();
clob.setString(1, req_param_xml);
statement.setClob(2, clob);