Using this code to update a record
var query = "UPDATE myTable SET name = #name where id = #id";
using (DbCommand cmd = new OleDbCommand(query, connection))
{
try
{
cmd.Parameters.Add(new OleDbParameter("#id", item.Id));
cmd.Parameters.Add(new OleDbParameter("#name", item.Name));
cmd.ExecuteNonQuery();
}
catch (Exception e) { }
}
But the record does not get updated. I get no error. The record exists in the DB. What is going on?
I don't know why but you have to add the parameters of your query in the order they appear in your query string. So flip these two lines
cmd.Parameters.Add(new OleDbParameter("#id", item.Id));
cmd.Parameters.Add(new OleDbParameter("#name", item.Name));
to
cmd.Parameters.Add(new OleDbParameter("#name", item.Name));
cmd.Parameters.Add(new OleDbParameter("#id", item.Id));
Related
Consider this code....
WHY is it failing telling me that the #PI_CDID parameter value is not set when trying to execute the stored procedure?
Console.WriteLine("Database Opened!");
SqlCommand cmd = new SqlCommand("P_IOU_Track", conn);
cmd.Parameters.Add(new SqlParameter("#PI_CDID", ICDID)); // parameter is added here
cmd.Parameters.Add(new SqlParameter("#PI_Title", ITitle));
cmd.Parameters.Add(new SqlParameter("#PI_Duration", IDuration));
cmd.Parameters.Add(new SqlParameter("#PI_CDNo", ICDNo));
cmd.Parameters.Add(new SqlParameter("#PI_TNo", ITNo));
foreach (SqlParameter p in cmd.Parameters )
{
Console.WriteLine("Parameter , {0} , Value --> {1} ",p.ParameterName, p.Value.ToString());
}
// Add output param
SqlParameter NextTID = new SqlParameter("#PO_NextTID", SqlDbType.BigInt);
NextTID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(NextTID);
// Execute procedure
cmd.ExecuteNonQuery();
You forgot cmd.CommandType = CommandType.StoredProcedure
Executing the below code gives me error. Column 'Username' cannot be null
Values are being passed in the variables. But I think the OdbcCommand statement is not prepared properly.
OdbcCommand cmd = new OdbcCommand
{
CommandText =
"INSERT INTO orders(username,name,email,address,contact_number,html_email)
VALUES(#username,#name,#email,#address,#contact_number,#html_email)",
Connection = Con
};
cmd.Parameters.AddWithValue("#username", username);
cmd.Parameters.AddWithValue("#name", name);
cmd.Parameters.AddWithValue("#email", email);
cmd.Parameters.AddWithValue("#address", add);
cmd.Parameters.AddWithValue("#contact_number", contact);
cmd.Parameters.AddWithValue("#html_email", table);
billid = cmd.ExecuteScalar().ToString();
OdbcCommand does not support named parameter syntax, instead you should use a question mark placeholder:
OdbcCommand cmd = new OdbcCommand
{
CommandText =
"INSERT INTO orders(username,name,email,address,contact_number,html_email)
VALUES(?, ?, ?, ?, ?, ?)",
Connection = Con
};
cmd.Parameters.AddWithValue("#p1", username);
cmd.Parameters.AddWithValue("#p2", name);
cmd.Parameters.AddWithValue("#p3", email);
cmd.Parameters.AddWithValue("#p4", add);
cmd.Parameters.AddWithValue("#p5", contact);
cmd.Parameters.AddWithValue("#p6", table);
int affectedRecords = cmd.ExecuteNonQuery();
Try This:
OdbcCommand cmd = new OdbcCommand("INSERT INTO orders(username,name,email,address,contact_number,html_email)
VALUES(#username,#name,#email,#address,#contact_number,#html_email)",Con);
cmd.Parameters.AddWithValue("#username", username);
cmd.Parameters.AddWithValue("#name", name);
cmd.Parameters.AddWithValue("#email", email);
cmd.Parameters.AddWithValue("#address", add);
cmd.Parameters.AddWithValue("#contact_number", contact);
cmd.Parameters.AddWithValue("#html_email", table);
billid = cmd.ExecuteNonQuery();
Use ExecuteNonQuery instead of ExecuteScalar() to execute the insert statement.
int affectedRows = cmd.ExecuteNonQuery();
You probably want to return the last inserted record Id, that's why you are using ExecuteScalar. OdbcCommand doesn't support then named parameters, so you need to use the placeholder in the query. So overall you need to change the query to this
CommandText = "INSERT INTO orders(username,name,email,address,contact_number,html_email)
VALUES(?,?,?,?,?,?) SELECT SCOPE_IDENTITY()";
Now you can use the ExecuteScalar()
billid = cmd.ExecuteScalar().ToString();
I have made a function like this
public int InsertData(CategoryPhoto catphoto)
{
string ans = null;
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "prcCategoryPhoto";
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.Add(new SqlParameter("#PhotoID", prdctphoto.PhotoID));
cmd.Parameters.Add(new SqlParameter("#PhotoName", catphoto.PhotoName));
//cmd.Parameters.Add(new SqlParameter("#LeftPhoto", prdctphoto.LeftPhoto));
//cmd.Parameters.Add(new SqlParameter("#RightPhoto", prdctphoto.RightPhoto));
//cmd.Parameters.Add(new SqlParameter("#BackPhoto", prdctphoto.BackPhoto));
//cmd.Parameters.Add(new SqlParameter("#MaterialPhoto", prdctphoto.MaterialPhoto));
cmd.Parameters.Add(new SqlParameter("#ExtName", catphoto.ExtName));
cmd.Parameters.Add(new SqlParameter("#PhotoType", catphoto.PhotoType));
cmd.Parameters.Add(new SqlParameter("#PhotoSize", catphoto.PhotoSize));
cmd.Parameters.Add(new SqlParameter("#CategoryID", catphoto.CategoryID));
ans = cmd.ExecuteScalar().ToString();
//var result = cmd.ExecuteScalar();
//ans = int.Parse(result.ToString());
cmd.Dispose();
DataConnection.CloseConnection();
return ans;
}
In my stored procedure is
create proc [dbo].[prcCategoryPhoto]
(
#PhotoName Varchar(100),
#ExtName Varchar(100),
#PhotoType Varchar(100),
#PhotoSize int,
#CategoryID varchar(20)
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID)
values (#PhotoName,#ExtName,#PhotoType,#PhotoSize,#CategoryID)
select ##IDENTITY
on writing return ans it is giving a error
can not implicitly convert string to int
and on writing
return int.Parse(ans);
it gives exception that nvarchar cannot be converted to int
now try this....
`
public string InsertData(CategoryPhoto catphoto)
{
string ans = null;
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "prcCategoryPhoto";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#PhotoName", catphoto.PhotoName));
cmd.Parameters.Add(new SqlParameter("#ExtName", catphoto.ExtName));
cmd.Parameters.Add(new SqlParameter("#PhotoType", catphoto.PhotoType));
cmd.Parameters.Add(new SqlParameter("#PhotoSize", catphoto.PhotoSize));
cmd.Parameters.Add(new SqlParameter("#CategoryID", catphoto.CategoryID));
ans = cmd.ExecuteScalar().ToString();
cmd.Dispose();
DataConnection.CloseConnection();
return ans;
}`
try below,
first alter your procedure, add a output parameter as below,
create proc [dbo].[prcCategoryPhoto]
(
#PhotoName Varchar(100),
#ExtName Varchar(100),
#PhotoType Varchar(100),
#PhotoSize int,
#CategoryID varchar(20),
#ID INT OUTPUT
)
as
insert into CategoryPhoto(PhotoName,ExtName,PhotoType,PhotoSize,CategoryID)
values (#PhotoName,#ExtName,#PhotoType,#PhotoSize,#CategoryID)
select #ID = ##IDENTITY
Next modify your function as below,
public int InsertData(CategoryPhoto catphoto)
{
SqlCommand cmd = DataConnection.GetConnection().CreateCommand();
cmd.CommandText = "prcCategoryPhoto";
cmd.CommandType = CommandType.StoredProcedure;
// cmd.Parameters.Add(new SqlParameter("#PhotoID", prdctphoto.PhotoID));
cmd.Parameters.Add(new SqlParameter("#PhotoName", catphoto.PhotoName));
//cmd.Parameters.Add(new SqlParameter("#LeftPhoto", prdctphoto.LeftPhoto));
//cmd.Parameters.Add(new SqlParameter("#RightPhoto", prdctphoto.RightPhoto));
//cmd.Parameters.Add(new SqlParameter("#BackPhoto", prdctphoto.BackPhoto));
//cmd.Parameters.Add(new SqlParameter("#MaterialPhoto", prdctphoto.MaterialPhoto));
cmd.Parameters.Add(new SqlParameter("#ExtName", catphoto.ExtName));
cmd.Parameters.Add(new SqlParameter("#PhotoType", catphoto.PhotoType));
cmd.Parameters.Add(new SqlParameter("#PhotoSize", catphoto.PhotoSize));
cmd.Parameters.Add(new SqlParameter("#CategoryID", catphoto.CategoryID));
cmd.Parameters.Add(new SqlParameter("#ID",System.Data.SqlDbType.Int));
cmd.Parameters["#ID"].Direction=System.Data.ParameterDirection.Output;
cmd.ExecuteNonQuery();
var ans = cmd.Parameters["#ID"].Value;
cmd.Dispose();
DataConnection.CloseConnection();
return Convert.ToInt32(ans);
}
Look at your string (ans) in debugger. Seems like you have some characters that can not be converted to int. Can you change the return type of the method from int to string?
You should be using the using keyword. It makes sure that the dispose method is called even if something goes wrong (i.e. and exception is raised).
Also, the ##Identity returns a numeric value in any case (according to MSDN), which should be convertible to an integer or a bigint. So my suggestion would be:
public Int64 InsertData(CategoryPhoto catphoto)
{
using (var connection = DataConnection.GetConnection)
{
using (var cmd = connection.CreateCommand())
{
cmd.CommandText = "prcCategoryPhoto";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#PhotoName", catphoto.PhotoName));
cmd.Parameters.Add(new SqlParameter("#ExtName", catphoto.ExtName));
cmd.Parameters.Add(new SqlParameter("#PhotoType", catphoto.PhotoType));
cmd.Parameters.Add(new SqlParameter("#PhotoSize", catphoto.PhotoSize));
cmd.Parameters.Add(new SqlParameter("#CategoryID", catphoto.CategoryID));
return (Int64)cmd.ExecuteScalar();
}
}
}
In case your identity-column is an integer, you may of course change the signature of the method to return an int instead.
Replace
public int InsertData(CategoryPhoto catphoto)
with
public string InsertData(CategoryPhoto catphoto)
and follow the same for its dependancies...
The ScoapIdentity will return your primarykey value , So that datatype is int , So you need to declare to int variable(not a string variable) .
So Replace this line string ans = null; to int ans = null;
and also need to change this below line
ans = Convert.ToInt32(cmd.ExecuteScalar());
I'm having an issue with an update query in C#. It's odd to me that I'm having an issue with update, but select, delete, and insert work.
public void updateTeacherInfo(string SSN, string Classroom, string salary, string tenured, string phone)
{
OracleConnection conn = new OracleConnection("myconnectionstring;");
conn.Open();
OracleCommand cmd = new OracleCommand("Update Teachers Set classroom_number = :TRM, Salary = :TSALARY, Tenured = :TTENURE, Phone_numer = :TPHONE Where SSN = :TSSN", conn);
OracleCommand commit = new OracleCommand("COMMIT", conn);
cmd.Parameters.Add(new OracleParameter(":TSSN", SSN));
cmd.Parameters.Add(new OracleParameter(":TRM", Classroom));
cmd.Parameters.Add(new OracleParameter(":TSALARY", salary));
cmd.Parameters.Add(new OracleParameter(":TTENURE", tenured));
cmd.Parameters.Add(new OracleParameter(":TPHONE", phone));
int i = cmd.ExecuteNonQuery();
int k = commit.ExecuteNonQuery();
MessageBox.Show(i + " rows affected");
MessageBox.Show(k + " rows affected");
conn.Close();
}
Edit* the rest of the method to clear things up, and also, it is not throwing any errors, but does not update the database.
Put the Parameters.Add in proper sequence. In your update query
"Update Teachers Set classroom_number = :TRM, Salary = :TSALARY, Tenured = :TTENURE, Phone_numer = :TPHONE Where SSN = :TSSN"
:TRM is occuring first and likewise. So keep the Parameters.Add also in same sequence. It will work. The sequence will be:
cmd.Parameters.Add(new OracleParameter(":TRM", Classroom));
cmd.Parameters.Add(new OracleParameter(":TSALARY", salary));
cmd.Parameters.Add(new OracleParameter(":TTENURE", tenured));
cmd.Parameters.Add(new OracleParameter(":TPHONE", phone));
cmd.Parameters.Add(new OracleParameter(":TSSN", SSN));
I have this error at the call af stored procedure... Why?
public void InsertVideo()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());
MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
cmd.Parameters.Add(new MySqlParameter("in_title", Title));
cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));
cmd.ExecuteNonQuery();
}
because you forget to OPEN the connection, before calling ExecuteNonQuery, call conn.Open() first,
conn.Open();
cmd.ExecuteNonQuery();
Your complete solution
public void InsertVideo()
{
MySqlConnection conn = new MySqlConnection(ConfigurationManager.ConnectionStrings["AxWaveConnection"].ToString());
MySqlCommand cmd = new MySqlCommand("InsertVideo", conn);
cmd.CommandType = System.Data.CommandType.StoredProcedure;
conn.Open()
cmd.Parameters.Add(new MySqlParameter("in_youtubevideoid", VideoId));
cmd.Parameters.Add(new MySqlParameter("in_title", Title));
cmd.Parameters.Add(new MySqlParameter("in_rating", ViewCount));
cmd.Parameters.Add(new MySqlParameter("in_viewcount", Rating));
cmd.ExecuteNonQuery();
}