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));
I'm having some 8 text boxes and one submit button. I just want to insert only one text box value into db when I press submit. So, I must press submit button 8 times to enter all the text box values into the db.
SqlConnection conn = new SqlConnection();
conn.ConnectionString = #"Data Source=Sadiq;Initial Catalog=rafi;Integrated Security=True";
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "student1";
int studid = Convert.ToInt32(TextBox1.Text);
string name = TextBox2.Text;
int age = Convert.ToInt32(TextBox3.Text);
string school = TextBox4.Text;
string clas = TextBox5.Text;
int marks = Convert.ToInt32(TextBox6.Text);
string grade = TextBox7.Text;
cmd.Parameters.Add(new SqlParameter("#stud_id", studid));
cmd.Parameters.Add(new SqlParameter("#name", name));
cmd.Parameters.Add(new SqlParameter("#age", age));
cmd.Parameters.Add(new SqlParameter("#school", school));
cmd.Parameters.Add(new SqlParameter("#class", clas));
cmd.Parameters.Add(new SqlParameter("#marks", marks));
cmd.Parameters.Add(new SqlParameter("#grade", grade));
cmd.Connection=conn;
cmd.ExecuteNonQuery();
conn.Close();
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());
First I tested simple cases:
cmd.Parameters.Add(new OracleParameter(":paramCode", OracleDbType.NVarchar2)).Value = userCode;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Codigo = :paramCode";
cmd.CommandType = System.Data.CommandType.Text;
OracleDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
//Works, retrieve data
}
Another:
cmd.Parameters.Add(new OracleParameter(":paramRole", OracleDbType.NVarchar2)).Value = userRole;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Role = :paramRole";
...
while (reader.Read())
{
//Also works
}
But when join, doesn't work.
cmd.Parameters.Add(new OracleParameter(":paramCode", OracleDbType.NVarchar2)).Value = userCode;
cmd.Parameters.Add(new OracleParameter(":paramRole", OracleDbType.NVarchar2)).Value = userRole;
cmd.CommandText = "SELECT * FROM VIEWUSERDATA WHERE Role = :paramRole AND Code = :paramCode";
...
while (reader.Read())//don't retrieve anything
{
}
Data exists, If I do the query in an external query editor(window) works fine.
Thanks.
By default, Oracle parameter binding is by position, not by name. Either swap the two cmd.Parameters.Add calls or set cmd.BindByName = true; before executing the query.
I have this update:
sql = "UPDATE table SET prioridade = #prioridade, situacao = #sit , responsavel = #resp , previsao_termino = #previsao, chamado_designado = #designado WHERE id = #id";
MySqlCommand cmd = new MySqlCommand();
cmd.CommandText = sql;
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add(new MySqlParameter("#prioridade", MySqlDbType.Int32)).Value = ch.Prioridade_ID;
cmd.Parameters.Add(new MySqlParameter("#sit", MySqlDbType.Int32)).Value = ch.Situacao_ID;
cmd.Parameters.Add(new MySqlParameter("#resp", MySqlDbType.Int32)).Value = ch.Responsavel_ID;
cmd.Parameters.Add(new MySqlParameter("#previsao", MySqlDbType.Date)).Value = ch.Previsao_Termino;
cmd.Parameters.Add(new MySqlParameter("#designado", MySqlDbType.Int32)).Value = ch.Chamado_Designado;
cmd.Parameters.Add(new MySqlParameter("#id", MySqlDbType.Int32)).Value = ch.ID;
_dal.Executar(cmd);
the value of ch.Previsao_Termino is equal to 31/05/2013 the field previsao_termino is a date type. When it will make the update it throws me an error saying that:
Wrong Value for the field previsao_termino 0031-05-2013.
Where did that 00 came from ? Maybe the connector ? I updated my connector to a new version, also i updated my VisualStudio 2010 to VisualStudio 2012 and sinced I changed that, i've got a lot of problems u.u.
Answer provided by #EdGibbs
When working with MySqlCommand.Parameters the variable you are passing as its value, MUST be the same type that you set the parameter. e.g
MySqlCommand.Parameter.Add(new MySqlParameter("#ParamName", MySqlDataType.DateTime)).value = dtValue
the varaible dtvalue MUST BE DATETIME TYPE, if like me you are using string, then you should use the following conversion.
DateTime.ParseExact(ch.Previsao_Termino, 'dd/MM/yyyy', null)