Adding parameters to PL/SQL in visual studio - c#

I have written a procedure-
create or replace procedure cv(x in int,y in int,z in varchar)
as
cursor c
is
select email,collegename,cgpa,compname
from student_cv
where (cgpa>=x and yearsofexp>=y) and compname=z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp values(tem.email,tem.collegename,tem.cgpa,tem.compname);
end loop;
end;
/
and now based on two combo box values(which the user inputs) and 1 local string constitute the parameters passed to my procedure.
This is the code I am using-
//DB_connect();
string oracleb = "Data source=HPPRO58; user ID=system; password=deb;";
conn = new OracleConnection(oracleb);
//conn.Open();
conn.Open();
String x3 = "google";
String x1 ;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
// System.Data.OracleClient.OracleCommand comm2 = new System.Data.OracleClient.OracleCommand();
OracleCommand comm2 = new OracleCommand();
String s2 = "cv";
comm2.Connection = conn;
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.Parameters.Add("z", System.Data.OracleClient.OracleType.VarChar).Value = x3;
//comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
but on running this code the error displayed is -
and line 59 is -
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
therefore I think there is some problem in declaring the parameters in my code

Related

OleDb Update database.mdb

When i use the CustomButton for to save the "Full_Name" in the Database [Rooms] => Person then there is just nothing happen. Also if i use the try & catch function, there will be no Exception.
The field in the Database stays Empty.
When i show the required variable in the MessageBox (idPlus2, Full_Name) then it throws me back the right informations.
So i think the problem must be in the UPDATE Sql string but i don't know whats wrong.
private string connstr = #"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=C:\....mdb";
//Path anonymous
string Full_Name;
[Obsolete]
private void customButton1_Click(object sender, EventArgs e)
{
conn = new OleDbConnection(connstr);
conn.Open();
strSQL = "SELECT * FROM [Guests] WHERE ID = ?";
cmd = new OleDbCommand(strSQL, conn);
da = new OleDbDataAdapter(cmd);
int id = CustomComboBox1.SelectedIndex;
int idPlus = id + 1;
cmd.Parameters.Add("?", idPlus);
OleDbDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Full_Name = reader["Vorname"].ToString() + ' ' + reader["Nachname"].ToString();
}
reader.Close();
string insertQuery = #"UPDATE [Rooms] SET Person = #Full_Name WHERE ID = ?";
cmd = new OleDbCommand(insertQuery, conn);
int id2 = customComboBox2.SelectedIndex;
int idPlus2 = id2 + 2;
cmd.Parameters.Add("?", idPlus2);
cmd.Parameters.Add(new OleDbParameter("#Full_Name", Full_Name));
cmd.CommandType = CommandType.Text;
cmd.ExecuteNonQuery();
conn.Close();
LoadTheme();
}
I have the answer
cmd.Parameters.Add("?", OleDbType.VarChar, 255).Value = CustomComboBox1.Texts;
cmd.Parameters.Add("?", idPlus2);
With OleDb you have to use ? for each variable or object which should be added to the database. That means that you can't specify the variable by name in the SQL string. You have to use the same order as the SQL string in C # code to insert the parameters.

connection closed when using cursor and procedure

My code is-
string oracleb = "Data source=HPPRO58; user ID=system; password=deb;";
conn = new OracleConnection(oracleb);
//conn.Open();
conn.Open();
String x3 = "google";
String x1 ;
String x2;
String s1 = "delete from temp";
OracleCommand comm = new OracleCommand(s1, conn);
comm.ExecuteNonQuery();
OracleCommand comm2 = new OracleCommand();
String s2 = "cv";
comm2.Connection = conn;
comm2.CommandText = s2;
comm2.CommandType = CommandType.StoredProcedure;
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;
comm2.Parameters.Add("y", System.Data.OracleClient.OracleType.Number).Value = comboBox2.Text;
comm2.Parameters.Add("z", System.Data.OracleClient.OracleType.Number).Value = x3;
comm2.ExecuteNonQuery();
String s3 = "select * from temp";
OracleCommand comm3 = new OracleCommand(s3, conn);
OracleDataAdapter MyAdapter3 = new OracleDataAdapter();//adapter acts as interface btw database and dataset(which is collectio of tables)
MyAdapter3.SelectCommand = comm;
DataTable dTable3 = new DataTable();//datatable represents a single table in database
MyAdapter3.Fill(dTable3);
dataGridView1.DataSource = dTable3;
conn.Close();
My error is-
I am trying to run a procedure which has a cursor too.Therefore I am using 3 comm variables.But I am getting the above error.Therefore I was wondering how to solve it.
I have opened the connection in the starting of the function but still it is showing connection is closed.
edit-
my procedure name is cv
new error-
my procedure-
create or replace procedure cv(x in int,y in int,z in varchar)
as
cursor c
is
select email,collegename,cgpa,compname
from student_cv
where (cgpa>=x and yearsofexp>=y) and compname=z;
tem c%rowtype;
begin
open c;
loop
fetch c into tem;
exit when c%notfound;
insert into temp values(tem.email,tem.collegename,tem.cgpa,tem.compname);
end loop;
end;
/
line 59 is-
comm2.Parameters.Add("x", System.Data.OracleClient.OracleType.Number).Value = comboBox1.Text;

The first entry of DataTable fills the whole access database

I have read an excel file to a DataTable but only the first row fills the whole MS Access database. I have confirmed that the data in the DataTable and in the Excel file are the same. My code is shown below:
using (OleDbConnection myCon = new OleDbConnection(#"Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" + outFilePath + ";Extended Properties=dBase IV"))
{
OleDbCommand cmd = new OleDbCommand();
string line = "001";
for (int n = 0; n < dt.Rows.Count; ++n)
{
if (String.IsNullOrEmpty(dt.Rows[n].Field<string>(0))) break;
cmd.CommandText = "INSERT INTO " + FileName +
"([JNL],[LINE],[TYPE],[DRACC],[CRACC],[EXPDRACC],[EXPCRACC]," +
"[DOCDATE],[REF],[DRAMT],[CRAMT]) VALUES (?,?,?,?,?,?,?,?,?,?,?)";
string inacc = dt.Rows[n].Field<string>(0);
string justNumbers = new String(inacc.Where(Char.IsDigit).ToArray());
string acc = String.Format("{0:##-######-#}", justNumbers);
string jnl = JNL;
string Ref = REF;
string type = TYPE;
int dramt = 0;
int cramt = 0;
if (type == "50") dramt = Convert.ToInt32(100 * dt.Rows[n].Field < double>(2));
else cramt = Convert.ToInt32(100 * dt.Rows[n].Field < double>(2));
cmd.Parameters.Add("#JNL", OleDbType.VarChar).Value = jnl;
cmd.Parameters.Add("#LINE", OleDbType.VarChar).Value = line;
cmd.Parameters.Add("#TYPE", OleDbType.VarChar).Value = type;
cmd.Parameters.Add("#DRACC", OleDbType.VarChar).Value = justNumbers;
cmd.Parameters.Add("#CRACC", OleDbType.VarChar).Value = justNumbers;
cmd.Parameters.Add("#EXPDRACC", OleDbType.VarChar).Value = acc;
cmd.Parameters.Add("#EXPCRACC", OleDbType.VarChar).Value = acc;
cmd.Parameters.Add("#DOCDATE", OleDbType.Date).Value = DateTime.Now;
cmd.Parameters.Add("#REF", OleDbType.VarChar).Value = Ref;
cmd.Parameters.Add("#DRAMT", OleDbType.Integer).Value = dramt;
cmd.Parameters.Add("#CRAMT", OleDbType.Integer).Value = cramt;
cmd.CommandType = CommandType.Text;
cmd.Connection = myCon;
myCon.Open();
cmd.ExecuteNonQuery();
myCon.Close();
}
}
How to resolve this kind of issue?
You have to remove the parameters from your command. You are adding ever more parameters to it. Use
cmd.Parameters.Clear();
as first line of your for statement.
You add the first set of parameters and reuse the same command without clearing them, so you end up with a command that has n*11 (parameters defined/loop).
The first match in the collection is used to substitute your commands params so you get n Entries with same data.

Grab SQL variables that changes in query in ADO.NET

How do I get variable #Test that was changed in query?
const string query = #"SET #Test = 2;";
using (var connection = new SqlConnection(conStr))
{
connection.Open();
var command = new SqlCommand(query, connection);
command.Parameters.AddWithValue("#Test", 1);
var r = command.ExecuteReader();
// command.Parameters["#Test"].Value == 1
// r hasn't any variables
}
ADDED:
I've solve this problem withoute creating stored procedure
const string query = #"SET #Test = 2;";
using (var connection = new SqlConnection(conStr))
{
connection.Open();
var command = new SqlCommand(query, connection);
SqlParameter par = command.Parameters.Add("#Test", SqlDbType.NVarChar, 15);
par.Direction = ParameterDirection.Output;
command.ExecuteNonQuery();
// par.Value now contain 2
}
Both ansvers help!
Firstly, in your stored procedure the parameter needs to be marked as OUTPUT
CREATE PROC MyQuery
#Test INT OUTPUT
AS
SET #Test = 2
Then, when constructing the c# code, instead of using AddWithValue, be more explicit in your creation of a SqlParameter, namely marking it as Input/Output.
var command = new SqlCommand("MyQuery", connection);
command.CommandType = CommandType.StoredProcedure;
var param = command.CreateParameter();
param.Name = "#Test";
param.Type = DbType.Int;
param.Direction = ParameterDirection.InputOutput;
param.Value = 1;
Now once you execute your command:
command.ExecuteNonQuery(); // Could also be ExecuteReader, if you have a resultset too!
You can read the value of param, which should have changed to 2
if(param.Value == 2)
{ Console.WriteLine("WooHoo"); }

SqlCeDataReader parameter ordinal = 1

I a Parameter ordial = 1 error on this code.
Can anyone explain it in this context? dbCon is correct as I can insert data to the database just trying out how to get it back no.
if (dbCon.State == ConnectionState.Closed)
{
dbCon.Open(); ;
}
SqlCeParameter vetidfromdropbox = new SqlCeParameter("#vetidfromdropbox", SqlDbType.Int);
vetidfromdropbox.Value = 2;
SqlCeCommand mySQLCommand = new SqlCeCommand("SELECT * FROM vets WHERE vetID = #vetidfromdropbox", dbCon);
SqlCeDataReader rdata = mySQLCommand.ExecuteReader();
if(rdata.Read()){
editNameTextbox.Text = (String)rdata["vetName"];
editSurnameTextbox.Text = (String)rdata["vetSurname"];
editCompanyNameTextbox.Text = (String)rdata["vetCompanyName"];
editPractiseAddTextBox.Text = (String)rdata["vetPractiseAddress"];
editMobileTextbox.Text = (String)rdata["vetMobile"];
editOtherTextbox.Text = (String)rdata["vetOther"];
editNotesTextbox.Text = (String)rdata["vetNotes"];
}else{
MessageBox.Show(" There has been an error with Read() ");
}
if (dbCon.State == ConnectionState.Open)
{
dbCon.Close();
}
You didn't add parameter to your SQL command:
var vetidfromdropbox = new SqlCeParameter("#vetidfromdropbox", SqlDbType.Int);
vetidfromdropbox.Value = 2;
var mySQLCommand = new SqlCeCommand(
"SELECT * FROM vets WHERE vetID = #vetidfromdropbox", dbCon);
mySQLCommand.Parameters.Add(vetidfromdropbox);
You can use AddWithValue to simplify the syntax:
var mySQLCommand = new SqlCommand(
"SELECT * FROM vets WHERE vetID = #vetidfromdropbox", dbCon);
mySQLCommand.Parameters.AddWithValue("vetidfromdropbox", 2);
Side note: use using on your sql connection and command to guarantee disposal and to simplify code.

Categories

Resources