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();
Related
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);
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);
Access 2003
VS 2010 C#
As subject title says I am having problems with. This is related to my previous question I asked, Here. I hope the mod's will be OK with this thread but I am not sure.
Martin Parkin advised not to close the connection between Insert and Select when using ##Identity with C# and MS-Access. I thought I got it working until I discovered that was not the case. To be honest I don't know how to solve this issue. So if anyone can help me I would appreciate it.
This is my btnLogin method..
cmd.CommandText = "INSERT INTO LoginLogTable (UserName, LoggedInDate, LoggedInTime) VALUES (#UserName, #LoggedInDate, #LoggedInTime)";
cmd.Parameters.AddWithValue("#UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("#LoggedInDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("#LoggedInTime", DateTime.Now.ToString("HH:mm"));
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
cmd.CommandText = "SELECT #ID = ##IDENTITY";
// cmd.Parameters.AddWithValue("#ID", OleDbType.WChar); << tried this, unsuccessful
int id = (int)cmd.ExecuteScalar(); // getting the same error?
myCon.Close();
This is my btnLogOut method...
OleDbCommand cmd = new OleDbCommand();
cmd.CommandType = CommandType.Text;
int id = 0;
cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = #LoggedOutDate, [LoggedOutTime] = #LoggedOutTime WHERE [ID] = #ID";
cmd.Parameters.AddWithValue("#ID", id);
cmd.Parameters.AddWithValue("#LoggedOutDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("#LoggedOutTime", DateTime.Now.ToString("HH:mm"));
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
Close();
Or
In the btnLogin method if I do
cmd.CommandText = "SELECT #ID = ##IDENTITY";
and hide the cmd.ExecuteNonQuery(); after it. Then date and time will get logged in the database but the date and time will not get saved in the database, for logging out.
I am not sure if the problem is with btnLogin method or btnLogOut method, or both.
Working Solution
Originally I did
cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = #LoggedOutDate,
[LoggedOutTime] = #LoggedOutTime WHERE [ID] = #ID";
cmd.Parameters.AddWithValue("#ID", id);
cmd.Parameters.AddWithValue("#LoggedOutDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("#LoggedOutTime", DateTime.Now.ToString("HH:mm"));
Then I did this
cmd.CommandText = " UPDATE [LoginLogTable] SET [UserName] = #UserName, [LoggedOutDate] =
#LoggedOutDate, [LoggedOutTime] = #LoggedOutTime WHERE ID = #ID";
cmd.Parameters.AddWithValue("#UserName", txtUserName.Text);
cmd.Parameters.AddWithValue("#LoggedOutDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("#LoggedOutTime", DateTime.Now.ToString("HH:mm"));
cmd.Parameters.AddWithValue("#ID", id);
Thanks to D Stanley and Gord Thompson.
The #ID variable does not persist in the database the way you seem to think it does. It will go out of scope when the connection is closed (possibly sooner). I would advise that you store the new identity within your application instead:
Assuming these are button handlers that are methods on the form, you could store the ID as a property of the form:
// somewhere in the form definition:
private int ID {get; set;}
...
cmd.CommandText = "SELECT ##IDENTITY";
int id = (int)cmd.ExecuteScalar();
this.ID = id;
Then use the ID in your Logout method:
// get the id from the form
int id = this.ID;
cmd.CommandText = " UPDATE [LoginLogTable] SET [LoggedOutDate] = #LoggedOutDate, [LoggedOutTime] = #LoggedOutTime WHERE [ID] = #ID";
cmd.Parameters.AddWithValue("#ID", id);
cmd.Parameters.AddWithValue("#LoggedOutDate", DateTime.Now.ToShortDateString());
cmd.Parameters.AddWithValue("#LoggedOutTime", DateTime.Now.ToString("HH:mm"));
I am using this method to insert a row into a table:
MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connect;
cmd.Connection.Open();
string commandLine = #"INSERT INTO Wanted (clientid,userid,startdate,enddate) VALUES" +
"(#clientid, #userid, #startdate, #enddate);";
cmd.CommandText = commandLine;
cmd.Parameters.AddWithValue("#clientid", userId);
cmd.Parameters.AddWithValue("#userid", "");
cmd.Parameters.AddWithValue("#startdate", start);
cmd.Parameters.AddWithValue("#enddate", end);
cmd.ExecuteNonQuery();
cmd.Connection.Close();
I hav also id column that have Auto Increment .
And i want to know if it possible to get the id that is created when i insert a new row.
You can access the MySqlCommand LastInsertedId property.
cmd.ExecuteNonQuery();
long id = cmd.LastInsertedId;
MySqlConnection connect = new MySqlConnection(connectionStringMySql);
MySqlCommand cmd = new MySqlCommand();
cmd.Connection = connect;
cmd.Connection.Open();
string commandLine = #"INSERT INTO Wanted (clientid,userid,startdate,enddate) "
+ "VALUES(#clientid, #userid, #startdate, #enddate);";
cmd.CommandText = commandLine;
cmd.Parameters.AddWithValue("#clientid", userId);
**cmd.Parameters["#clientid"].Direction = ParameterDirection.Output;**
cmd.Parameters.AddWithValue("#userid", "");
cmd.Parameters.AddWithValue("#startdate", start);
cmd.Parameters.AddWithValue("#enddate", end);
cmd.ExecuteNonQuery();
cmd.Connection.Close();
Basically you should add this to end of your CommandText:
SET #newPK = LAST_INSERT_ID();
and add another ADO.NET parameter "newPK". After command is executed it will contain new ID.
con.Open();
SqlCommand cmd=new SqlCommand("INSERT INTO user(Firstname,Lastname,Email,Pass,Type)
values(#first,#last,#email,#pass,#type)",con);
cmd.Parameters.Add("#first",SqlDbType.NVarChar).Value = txtfirst.Text;
cmd.Parameters.Add("#last",SqlDbType.NVarChar).Value = txtlast.Text;
cmd.Parameters.Add("#email",SqlDbType.NVarChar).Value = txtemail.Text;
cmd.Parameters.Add("#pass",SqlDbType.NVarChar).Value = txtpass.Text;
cmd.Parameters.Add("#type",SqlDbType.NVarChar).Value = "customer";
cmd.ExecuteNonQuery();
con.Close();
what is the problem with my syntax it says "Incorrect syntax near the keyword 'user'."
you should escape the table name user with delimited identifiers,
SqlCommand cmd=new SqlCommand("INSERT INTO [user] (Firstname,Lastname,Email,Pass,Type) values(#first,#last,#email,#pass,#type)",con);
SQL Server Reserved Keywords
SQL Server Delimited Identifiers
UPDATE 1
Refractor your code by
using using statement to properly dispose objects
using Try-Catch block to properly handle exceptions
code snippet:
string _connStr = "connectionString here";
string _query = "INSERT INTO [user] (Firstname,Lastname,Email,Pass,Type) values (#first,#last,#email,#pass,#type)";
using (SqlConnection conn = new SqlConnection(_connStr))
{
using (SqlCommand comm = new SqlCommand())
{
comm.Connection = conn;
comm.CommandType = CommandType.Text;
comm.CommandText = _query;
comm.Parameters.AddWithValue("#first", txtfirst.Text);
comm.Parameters.AddWithValue("#last", txtlast.Text);
comm.Parameters.AddWithValue("#email", txtemail.Text);
comm.Parameters.AddWithValue("#pass", txtpass.Text);
comm.Parameters.AddWithValue("#type", "customer");
try
{
conn.Open();
comm.ExecuteNonQuery();
}
catch(SqlException ex)
{
// other codes here
// do something with the exception
// don't swallow it.
}
}
}
AddWithValue
Add (recommended one)
USER is a reserved keyword on SQL Server.
You should use your table name with brackets [] like;
INSERT INTO [user]
You can try like;
con.Open();
SqlCommand cmd=new SqlCommand("INSERT INTO [user] (Firstname,Lastname,Email,Pass,Type) values(#first,#last,#email,#pass,#type)",con);
cmd.Parameters.AddWithValue("#first", txtfirst.Text);
cmd.Parameters.AddWithValue("#last", txtlast.Text);
cmd.Parameters.AddWithValue("#email", txtemail.Text);
cmd.Parameters.AddWithValue("#pass", txtpass.Text);
cmd.Parameters.AddWithValue("#type", "customer");
cmd.ExecuteNonQuery();
con.Close();
And also like #JW said, it is always a good approach to using them in a try-catch statement.
Best Practices of Exception Management