Session variable as a SqlParameter - c#

I've got this all except for my...
cmd.Parameters["#CAssetID"].Value.ToString();
Line of code where I am trying to assign the session variable to SqlParameter to pass it in to a stored procedure. I just can't seem to get the syntax right. I've tried several different things here is my last attempt.
SqlConnection RejectConnecton = new SqlConnection("Data Source=GBAPTCCSR01;Initial Catalog=CInTracDB;Integrated Security=True");
SqlCommand cmd = new SqlCommand();
SqlDataReader reader;
cmd.CommandText = "usp_RejectSubmission";
cmd.Parameters["#CAssetID"].Value.ToString();
cmd.CommandType = CommandType.StoredProcedure;
cmd.Connection = RejectConnecton;
RejectConnecton.Open();
reader = cmd.ExecuteReader();
RejectConnecton.Close();
Thanks for any help you can offer!

cmd.Parameters.AddWithValue("#CAssetID", Session["YourSessionParamName"]);

Related

oracle function always return null

I'm using the flowing code to call the oracle function with the return value,
but it returns null always
OracleCommand cmd = new OracleCommand();
using (OracleConnection cnn = new OracleConnection("Data Source=(DESCRIPTION=(ADDRESS=(PROTOCOL=TCP)(HOST=321352427544)(PORT=1521))(CONNECT_DATA=(SERVICE_NAME=test)));User ID=abc;Password=123;"))
{
cmd.Connection = cnn;
cmd.CommandText = "GetEmp";
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add("P_EMP_ID", OracleDbType.Int32).Value = 4241;
cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;
cnn.Open();
cmd.ExecuteNonQuery();
string Count = (string)cmd.Parameters["return_value"].Value;
cnn.Close();
}
the problem was solved in two ways by
add cmd.BindByName = true;
add the return parameter in the first: cmd.Parameters.Add(new OracleParameter("return_value", OracleDbType.Int32)).Direction = ParameterDirection.ReturnValue;

No mapping exists from object type System.Windows.Forms.DateTimePicker

When I press the register button, a MessageBox will show up and then this happens:
No mapping exists from object type System.Windows.Forms.DateTimePicker to a known managed provider native type.
How do I fix this?
SqlConnection sqlcon = new SqlConnection();
sqlcon.ConnectionString = #"Data Source=MYCOMPUTER-PC\SQLEXPRESS;Database=COMPPROG;User
Id=user;Password=password";
SqlCommand scmd = new SqlCommand("SELECT COUNT (*) as cnt from USERACCOUNT", sqlcon);
SqlCommand cmd = sqlcon.CreateCommand();
cmd.CommandType = CommandType.Text;
cmd.CommandText = "INSERT INTO USERACCOUNT
(FName,MName,Sname,TxtBirth,comboGender,textBox8,textBox1,textBox2)
VALUES(#FName,#MName,#Sname,#TxtBirth,#comboGender,#textBox8,#textBox1,#textBox2)";
cmd.Parameters.AddWithValue("#FName", FName.Text);
cmd.Parameters.AddWithValue("#MName", MName.Text);
cmd.Parameters.AddWithValue("#Sname", Sname.Text);
cmd.Parameters.AddWithValue("#TxtBirth", TxtBirth);
cmd.Parameters.AddWithValue("#textBox8", textBox8.Text);
cmd.Parameters.AddWithValue("#textBox1", textBox1.Text);
cmd.Parameters.AddWithValue("#textBox2", textBox2.Text);
cmd.Parameters.AddWithValue("#comboGender", comboGender);
sqlcon.Open();
MessageBox.Show("Record added sucessfully!", "Registration Success!");
cmd.ExecuteNonQuery();
sqlcon.Close();
Like #gunr2171 said in comments problem is from this line so change it like this :
cmd.Parameters.AddWithValue("#TxtBirth", TxtBirth.Value);
This is value of TxtBirth for your query.
Edit :
And your error means you are passing the DatePicker itself and it can not be done.

i want to make update in two table in one button button1_click

I have two tables, Projects and ProjectImages. I want to get the image_id from the second table. This is my current code:
SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Waves;Integrated Security=true;");
SqlCommand com = new SqlCommand("Update_Project", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#ProjectID",Label1.Text);
com.Parameters.AddWithValue("#ProjectName",txtName.Text);
com.Parameters.AddWithValue("#VideoUrl", txtName.Text);
com.Parameters.AddWithValue("#Notes", txtName.Text);
com.Parameters.AddWithValue("#Date",DateTime.Now.Date);
WavesEntities wee = new WavesEntities();
var query = from p in wee.Project_Images
select p.img_Id;
SqlCommand com1 = new SqlCommand("UpdateProjectImages", con);
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#img_id", query.AsEnumerable());
com.Parameters.AddWithValue("#img_path", con);
con.Open();
com.ExecuteNonQuery();
con.Close();
the error:
No mapping exists from object type
System.Data.Objects.ObjectQuery`1[[System.Int32, mscorlib,
Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089]] to
a known managed provider native type.
EDIT: After seeing your edit, your problem might be a different issue, but still, you can try what I answered.
EDIT 2: Your parameters in command 2 are referencing the command 1. Change your parameters to this:
com1.Parameters.AddWithValue("#img_id", query.AsEnumerable());
com1.Parameters.AddWithValue("#img_path", con);
Your problem is that you cannot use the same SqlConnection for two "alive" SqlCommands.
Change your code to something like this:
using(SqlConnection con = new SqlConnection("Data Source=.;Initial Catalog=Waves;Integrated Security=true;"))
{
con.Open();
using(SqlCommand com = new SqlCommand("Update_Project", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#ProjectID",Label1.Text);
com.Parameters.AddWithValue("#ProjectName",txtName.Text);
com.Parameters.AddWithValue("#VideoUrl", txtName.Text);
com.Parameters.AddWithValue("#Notes", txtName.Text);
com.Parameters.AddWithValue("#Date",DateTime.Now.Date);
WavesEntities wee = new WavesEntities();
var query = from p in wee.Project_Images
select p.img_Id;
}
using(SqlCommand com1 = new SqlCommand("UpdateProjectImages", con))
{
com.CommandType = CommandType.StoredProcedure;
com.Parameters.AddWithValue("#img_id", query.AsEnumerable());
com.Parameters.AddWithValue("#img_path", con);
con.Open();
com.ExecuteNonQuery();
}
}
I think you need to get the img_id for the respective ProjectID from wee.Project_Images method and then pass the single retrieved value (img_id) to the stored procedure for update. So your query would become
var query1 = from p in wee.Project_Images
where p.ProjectID == Label1.Text
select p.img_id;
com.Parameters.AddWithValue("#img_id", query);
Please note that you can not pass an Enumerable() object as a parameter to a stored procedure. Also don't forget to use using statement for SqlConnection and SqlCommand object for better disposal of resources after it's been used.

Query using Oracle Parameter returns no result 11g

A simple select statement using variable binding gives no rows??
OracleConnection con = new OracleConnection(constr);
con.Open();
OracleCommand cmd = new OracleCommand();
cmd.Connection = con;
cmd.CommandText = "select country_name from hr.countries where country_id = :country_id;
OracleParameter p_country_id = new OracleParameter();
p_country_id.OracleDbType = OracleDbType.Varchar2;
p_country_id.Value = "UK";
cmd.Parameters.Add(p_country_id);
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{} ---> no rows
tried adding parameterName ,direction,size still result is 0???
Any help??
I believe this is all correct but it is written in NotePad. At the very least it should get you on the right track.
using (OracleConnection con = new OracleConnection(constr))
{
con.Open();
using (OracleCommand cmd = con.CreateCommand())
{
cmd.CommandText = "select country_name from hr.countries where country_id = :country_id";
cmd.Parameters.Add("country_id", "UK")
OracleDataReader dr = cmd.ExecuteReader();
if (dr.Read())
{
// You code here
}
}
}
NOTE: I put the using statements in there because this is always recommended when executing database queries. If an exception occurs the using statement will guarantee your database connection is still closed.
You forgot to give your parameter a name. Do so and it should work:
p_country_id.ParameterName = "country_id";
Thanks for responding guys, My unit test project app.config was pointing to wrong DB schema :(
Grrrrrrrrrrrrr! can't get anything stupid then this :) i will blame on my flu :)

SQLParameter not working properly

I am trying to access a stored procedure and I'm getting an error that says:
Procedure or function 'getbug' expects parameter '#bugID', which was not supplied.
This is my code to call the procedure.
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("bugID", bugID));
bugID is set as 1089 (and is type int)
I can't figure out why this won't work.
Try this instead
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.Add(new SqlParameter("#bugID", bugID));
Try adding the "#" to the parameter
SqlCommand cmd = new SqlCommand("getbug", cn);
cmd.Parameters.Add(new SqlParameter("#bugID", bugID));
More code will help. Here's a couple of ideas though.
If you're calling a stored procedure, you need to specify the CommandType. Also, you can use the AddWithValue method to shorten your code.
using (var cn = new SqlConnection("your connection string"))
{
using (var cmd = new SqlCommand("getbug", cn))
{
cmd.CommandType = CommandType.StoredProcedure;
cmd.Parameters.AddWithValue("#bugID", bugID);
//etc...
}
}

Categories

Resources