I wanted to learn which usage is true?
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
OR
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString()));
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
This is passing a parentID to parameter #ParentSesID.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
is passing a null value to parameter.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString()));
is passing equal to string.Empty, which is not allowed in numerical data types.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", null);
is same as ignoring the parameter.
So when you need to pass null to SP you've to pass DBNull.Value.
This one: cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value)); is a correct, in case the field you are writing to can accept NULL value in database.
This one: cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString())); if the field is a, say, VARCHAR, and does not accept NULL, but you want to indicate in some way an absence of value in that field, so you write inside your application specific "no value" value. So your application knows, if it founds that value in the field, that means: no value.
DBNull.Value.ToString() will return empty string, so I think
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
is a good approach and here the only approach
see here:
http://ideone.com/iGo1Jh
Related
I'm testing oracle function calls from c#.
I've managed to write stable working test function and it's call.
here it is.
create or replace function abs.test_func(test_in in integer,test_varchar in varchar2)
return integer
is
test_out integer ;
BEGIN
test_out:=test_in;
RETURN test_out;
END;
c# call
using (var cmd = new OracleCommand("abs.test_func", conn1))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("test_in", OracleDbType.Int64, test_in, ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("test_varchar", OracleDbType.Varchar2, 2000, test_varchar, ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("test_out", OracleDbType.Int64, ParameterDirection.ReturnValue));
cmd.ExecuteNonQuery();
JsonResponse.Result = Int32.Parse(cmd.Parameters[0].Value.ToString());
This code works perfectly.
Now I'm just doing the same, just put varchar2 input in front , before integer input.
create or replace function abs.test_func(test_varchar in varchar2, test_in in integer)
return integer
is
test_out integer ;
BEGIN
test_out:=test_in;
RETURN test_out;
END;
c# call
using (var cmd = new OracleCommand("abs.test_func", conn1))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new OracleParameter("test_varchar", OracleDbType.Varchar2, 2000, test_varchar, ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("test_in", OracleDbType.Int64, test_in, ParameterDirection.Input));
cmd.Parameters.Add(new OracleParameter("test_out", OracleDbType.Int64, ParameterDirection.ReturnValue));
cmd.ExecuteNonQuery();
JsonResponse.Result = Int32.Parse(cmd.Parameters[0].Value.ToString());
And my c# throughs error
Input string was not in a correct format.
How can switching variables effect the outcome ????
I had a similar bug, and it was resolved with
alter system set "_qkslvc_extended_bind_sz"=0 scope=spfile;
and restart database.
Probably it was fixed already in newer oracle versions.
What version do you use?
I am trying to pass null value to SqlParameter if the value of variable vName is null, else pass vName value as shown below
cmd.Parameters.Add(new SqlParameter("#NAME", SqlDbType.VarChar)).Value = vName ? null : DBNull.Value;
but I get an error
Cannot implicitly convert type 'string' to 'bool'
I searched and found that I have to use AgeItem.AgeIndex but I got error that says
The name 'AgeItem' does not exist in the current context
cmd.Parameters.Add(new SqlParameter("#NAME", SqlDbType.VarChar)).Value =(object)AgeItem.AgeIndex ?? DBNull.Value;
vName is a string but you use it like it was a bool: vName ? .... Use vName == null ? ...:
cmd.Parameters.Add(new SqlParameter("#NAME", SqlDbType.VarChar)).Value =
vName == null ? DBNull.Value : (Object)vName;
IMHO much cleaner:
if (vName != null)
cmd.Parameters.AddWithValue("#NAME",vName);
else
cmd.Parameters.AddWithValue("#Name",null);
Code should be change as follow
cmd.Parameters.Add(new SqlParameter("#NAME", SqlDbType.VarChar)).Value!= null ? vName: DBNull.Value;
I wanted to learn which usage is true?
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
OR
if(!string.IsNullOrEmpty(parentID))
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
else
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString()));
cmd.Parameters.Add(new SqlParameter("#ParentSesID", parentID));
This is passing a parentID to parameter #ParentSesID.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
is passing a null value to parameter.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString()));
is passing equal to string.Empty, which is not allowed in numerical data types.
cmd.Parameters.Add(new SqlParameter("#ParentSesID", null);
is same as ignoring the parameter.
So when you need to pass null to SP you've to pass DBNull.Value.
This one: cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value)); is a correct, in case the field you are writing to can accept NULL value in database.
This one: cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value.ToString())); if the field is a, say, VARCHAR, and does not accept NULL, but you want to indicate in some way an absence of value in that field, so you write inside your application specific "no value" value. So your application knows, if it founds that value in the field, that means: no value.
DBNull.Value.ToString() will return empty string, so I think
cmd.Parameters.Add(new SqlParameter("#ParentSesID", DBNull.Value));
is a good approach and here the only approach
see here:
http://ideone.com/iGo1Jh
My query returns false when some values are null, but my table allows Null values.
What did I do wrong?
cmd.CommandText ="Insert into BusinessTbl(BName,BAddress,BEmail,BMobile,BPhone,Cat_Id)" +
"values(#bname,#baddress,#bemail,#bmobile,#bphone,#catid)";
cmd.Parameters.AddWithValue("#bname", b.BusinessName);
cmd.Parameters.AddWithValue("#name", b.BusinessAddress);
cmd.Parameters.AddWithValue("#bemail", b.BusinessEmail);
cmd.Parameters.AddWithValue("#bmobile", b.BusinessMobile);
cmd.Parameters.AddWithValue("#bphone", b.BusinessPhone);
cmd.Parameters.AddWithValue("#catid", b.ddlbcategory);
con.ExecuteNonQuery(cmd);
My Table
This is a vexing feature of ADO.NET parameters; basically:
cmd.Parameters.AddWithValue("#bname", ((object)b.BusinessName) ?? DBNull.Value);
cmd.Parameters.AddWithValue("#name", ((object)b.BusinessAddress) ?? DBNull.Value);
// etc
should fix you. If the .Value is null, the parameter isn't sent - it needs to be DBNull.Value. Alternatively, a tool like "Dapper" helps avoid this pain:
con.Execute(#"Insert into BusinessTbl(BName,BAddress,BEmail,BMobile,BPhone,Cat_Id)
values(#bname,#baddress,#bemail,#bmobile,#bphone,#catid)",
new { bname = b.BusinessName, ... , catid = b.ddlbcategory });
(which will parameterize correctly, including the nulls)
I'm making a form in visual studio that's able to access a database, fill a datatable with the db's table, display the datatable in a datagridview, populate editable fields on screen, and then pass those values of those fields back into the database to their respective columns. My problem is happening on the last step of that, as my update function doesn't seem to be working. I've searched around, but I'm not really sure where I'm going wrong.
string query = "UPDATE Actors SET fname=#fName, lname=#lName, sex=#sex, age=#age, action=#action, comic=#comic, drama=#drama, ego=#ego, music=#music, starQual=#starQual, romance=#romance, sexApp=#sexApp, workEth=#workEth, popularity=#popularity WHERE Id=#Id";
SQLiteConnection myConnection = new SQLiteConnection("Data Source=data.db");
SQLiteCommand cmd = new SQLiteCommand(query, myConnection);
myConnection.Open();
cmd.Parameters.Add(new SQLiteParameter("#fname", enterfName.Text));
cmd.Parameters.Add(new SQLiteParameter("#lname", enterlName.Text));
cmd.Parameters.Add(new SQLiteParameter("#sex", enterSex.Text));
cmd.Parameters.Add(new SQLiteParameter("#age", Convert.ToInt32(enterAge.Value)));
cmd.Parameters.Add(new SQLiteParameter("#action", Convert.ToInt32(enterAction.Value)));
cmd.Parameters.Add(new SQLiteParameter("#comic", Convert.ToInt32(enterComic.Value)));
cmd.Parameters.Add(new SQLiteParameter("#drama", Convert.ToInt32(enterDrama.Value)));
cmd.Parameters.Add(new SQLiteParameter("#ego", Convert.ToInt32(enterEgo.Value)));
cmd.Parameters.Add(new SQLiteParameter("#music", Convert.ToInt32(enterMusic.Value)));
cmd.Parameters.Add(new SQLiteParameter("#starQual", Convert.ToInt32(enterStarQual.Value)));
cmd.Parameters.Add(new SQLiteParameter("#romance", Convert.ToInt32(enterRomance.Value)));
cmd.Parameters.Add(new SQLiteParameter("#sexApp", Convert.ToInt32(enterSexApp.Value)));
cmd.Parameters.Add(new SQLiteParameter("workEth", Convert.ToInt32(enterWorkEth.Value)));
cmd.Parameters.Add(new SQLiteParameter("#popularity", Convert.ToInt32(enterPopularity.Value)));
cmd.Parameters.Add(new SQLiteParameter("#Id", Convert.ToInt32(indexHold.Value)));
cmd.ExecuteNonQuery();
myConnection.Close();
//Update gridview with the changes
updateGridView();
You forgot the # of the parameter
cmd.Parameters.Add(new SQLiteParameter("#workEth",Convert.ToInt32(enterWorkEth.Value)));