Executing procedure from Visual Studio









up vote
-1
down vote

favorite












I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?



DB_connect();

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();
String s2 = "exec cv";

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.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();


This is my stored 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;
/


Edit:



enter image description here










share|improve this question























  • Have you tested that the procedure actually returns any values?
    – stuartd
    2 days ago










  • my procedure is working in sql plus but I'm getting this error while executing this from visual studio
    – portgas. D. Ace
    2 days ago






  • 2




    On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
    – GolezTrol
    2 days ago










  • Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
    – Igor
    2 days ago















up vote
-1
down vote

favorite












I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?



DB_connect();

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();
String s2 = "exec cv";

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.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();


This is my stored 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;
/


Edit:



enter image description here










share|improve this question























  • Have you tested that the procedure actually returns any values?
    – stuartd
    2 days ago










  • my procedure is working in sql plus but I'm getting this error while executing this from visual studio
    – portgas. D. Ace
    2 days ago






  • 2




    On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
    – GolezTrol
    2 days ago










  • Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
    – Igor
    2 days ago













up vote
-1
down vote

favorite









up vote
-1
down vote

favorite











I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?



DB_connect();

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();
String s2 = "exec cv";

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.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();


This is my stored 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;
/


Edit:



enter image description here










share|improve this question















I'm trying to execute plsql procedure from Visual Studio. Basically what I'm trying to do is pass some parameters and using them I'm trying to insert values into a different table. The procedure uses a cursor and inserts values into a table and after that I'm deleting that table to make use of it for the next time. The table obtained using cursor needs to be finally displayed on gridbox. But the below program isn't displaying anything. Can someone help me with this?



DB_connect();

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();
String s2 = "exec cv";

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.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();


This is my stored 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;
/


Edit:



enter image description here







c# sql .net oracle visual-studio






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited 2 days ago









marc_s

564k12510881240




564k12510881240










asked 2 days ago









portgas. D. Ace

123




123











  • Have you tested that the procedure actually returns any values?
    – stuartd
    2 days ago










  • my procedure is working in sql plus but I'm getting this error while executing this from visual studio
    – portgas. D. Ace
    2 days ago






  • 2




    On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
    – GolezTrol
    2 days ago










  • Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
    – Igor
    2 days ago

















  • Have you tested that the procedure actually returns any values?
    – stuartd
    2 days ago










  • my procedure is working in sql plus but I'm getting this error while executing this from visual studio
    – portgas. D. Ace
    2 days ago






  • 2




    On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
    – GolezTrol
    2 days ago










  • Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
    – Igor
    2 days ago
















Have you tested that the procedure actually returns any values?
– stuartd
2 days ago




Have you tested that the procedure actually returns any values?
– stuartd
2 days ago












my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago




my procedure is working in sql plus but I'm getting this error while executing this from visual studio
– portgas. D. Ace
2 days ago




2




2




On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago




On a general note, it's better to just run a query, or a view, or even to make a function that returns an object/rowtype, or a table of objects, or even a refcursor which you can open and read. Writing temporary data just for the sake of reading it on the other side should be the last fallback.
– GolezTrol
2 days ago












Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
– Igor
2 days ago





Open your connection: conn.Open(); before you execute any operations against the data store. Also it is not clear where conn resides. Is this a global (static) field somewhere? If so maybe there is a race condition where multiple threads are trying to open/close this connection at the same time (like a web application with multiple requests).
– Igor
2 days ago













3 Answers
3






active

oldest

votes

















up vote
1
down vote













If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.



-edit-



While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.



Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)






share|improve this answer






















  • the connection is opened in the Db_connect function
    – portgas. D. Ace
    2 days ago







  • 1




    Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
    – GolezTrol
    2 days ago











  • no It's connected ,I checked it in data connections
    – portgas. D. Ace
    2 days ago










  • And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
    – GolezTrol
    yesterday

















up vote
0
down vote













Your approach is not really the "direct path", I would do it like this. Either select the table directly:



var s1 = "select email, collegename, cgpa, compname from student_cv 
where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
OracleCommand comm = new OracleCommand(s1, conn);
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";

OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;


Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:



OracleDataReader dr = comm.ExecuteReader();
while ( dr.Read() )
// loop through the rows and process the rows one-by-one

dr.close();


The other way is to use a function or procedure like this:



create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
res SYS_REFCURSOR;
BEGIN
OPEN res FOR
select email, collegename, cgpa, compname
from student_cv
where (cgpa >= x and yearsofexp >= y) and compname = z;

RETURN res;
end;


And then call it in C# as this:



comm.CommandType = CommandType.Text;
comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

OracleDataAdapter da = new OracleDataAdapter(comm);
DataTable dt = new DataTable();
da.Fill(dt);
dataGridView1.DataSource = dt;


Of course, you can also use OracleDataReader like above.



Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.



Regarding your actual error message, ensure the correct order:



  1. open the connection

  2. execute the command (or several commands)

  3. close the connection

As OracleConnection implements IDisposal you should enclose it by using like this



using (var conn = new OracleConnection() ) 
conn.ConnectionString = "Data Source=...";
conn.Open();
// Code from above

conn.Close();



OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using






share|improve this answer



























    up vote
    -4
    down vote













    You may need a commit; before end; ?






    share|improve this answer




















      Your Answer






      StackExchange.ifUsing("editor", function ()
      StackExchange.using("externalEditor", function ()
      StackExchange.using("snippets", function ()
      StackExchange.snippets.init();
      );
      );
      , "code-snippets");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "1"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: true,
      noModals: true,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: 10,
      bindNavPrevention: true,
      postfix: "",
      imageUploader:
      brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
      contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
      allowUrls: true
      ,
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229022%2fexecuting-procedure-from-visual-studio%23new-answer', 'question_page');

      );

      Post as a guest






























      3 Answers
      3






      active

      oldest

      votes








      3 Answers
      3






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      1
      down vote













      If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.



      -edit-



      While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.



      Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)






      share|improve this answer






















      • the connection is opened in the Db_connect function
        – portgas. D. Ace
        2 days ago







      • 1




        Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
        – GolezTrol
        2 days ago











      • no It's connected ,I checked it in data connections
        – portgas. D. Ace
        2 days ago










      • And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
        – GolezTrol
        yesterday














      up vote
      1
      down vote













      If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.



      -edit-



      While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.



      Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)






      share|improve this answer






















      • the connection is opened in the Db_connect function
        – portgas. D. Ace
        2 days ago







      • 1




        Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
        – GolezTrol
        2 days ago











      • no It's connected ,I checked it in data connections
        – portgas. D. Ace
        2 days ago










      • And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
        – GolezTrol
        yesterday












      up vote
      1
      down vote










      up vote
      1
      down vote









      If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.



      -edit-



      While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.



      Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)






      share|improve this answer














      If you set the type to stored procedure, all you need is the name of the stored procedure. So commandtext should be cv rather than exec cv.



      -edit-



      While the thing above is an issue as well, the screenshot you posted in your update suggests that the connection is not opened yet when you call ExecuteNonQuery.



      Or maybe it is opened, but not assigned to the command. Since an application could theoretically have multiple connections, you have to tell the command which one to use. There is no global reference to the connection that the command will use implicitly. The example given in Oracle's documentation shows how this could be done through a property, although apparently you can also set it in the constructor, which you do for connections 1 and 3 but not for connection 2. I guess there is the problem. :o)







      share|improve this answer














      share|improve this answer



      share|improve this answer








      edited yesterday

























      answered 2 days ago









      GolezTrol

      96.7k9127170




      96.7k9127170











      • the connection is opened in the Db_connect function
        – portgas. D. Ace
        2 days ago







      • 1




        Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
        – GolezTrol
        2 days ago











      • no It's connected ,I checked it in data connections
        – portgas. D. Ace
        2 days ago










      • And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
        – GolezTrol
        yesterday
















      • the connection is opened in the Db_connect function
        – portgas. D. Ace
        2 days ago







      • 1




        Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
        – GolezTrol
        2 days ago











      • no It's connected ,I checked it in data connections
        – portgas. D. Ace
        2 days ago










      • And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
        – GolezTrol
        yesterday















      the connection is opened in the Db_connect function
      – portgas. D. Ace
      2 days ago





      the connection is opened in the Db_connect function
      – portgas. D. Ace
      2 days ago





      1




      1




      Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
      – GolezTrol
      2 days ago





      Yes, but look at the error. It says 'connection is closed'. This error is not in the stored proc, and maybe not even in the way you call the stored proc (apart from the thing I mentioned), but in the fact that your application (or your command or query to be precise) doesn't have a database connection.
      – GolezTrol
      2 days ago













      no It's connected ,I checked it in data connections
      – portgas. D. Ace
      2 days ago




      no It's connected ,I checked it in data connections
      – portgas. D. Ace
      2 days ago












      And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
      – GolezTrol
      yesterday




      And is the connection assigned to the command? Looking at the code again, it seems you're not assigning it to comm2.
      – GolezTrol
      yesterday












      up vote
      0
      down vote













      Your approach is not really the "direct path", I would do it like this. Either select the table directly:



      var s1 = "select email, collegename, cgpa, compname from student_cv 
      where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
      OracleCommand comm = new OracleCommand(s1, conn);
      comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
      comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
      comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";

      OracleDataAdapter da = new OracleDataAdapter(comm);
      DataTable dt = new DataTable();
      da.Fill(dt);
      dataGridView1.DataSource = dt;


      Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:



      OracleDataReader dr = comm.ExecuteReader();
      while ( dr.Read() )
      // loop through the rows and process the rows one-by-one

      dr.close();


      The other way is to use a function or procedure like this:



      create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
      res SYS_REFCURSOR;
      BEGIN
      OPEN res FOR
      select email, collegename, cgpa, compname
      from student_cv
      where (cgpa >= x and yearsofexp >= y) and compname = z;

      RETURN res;
      end;


      And then call it in C# as this:



      comm.CommandType = CommandType.Text;
      comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
      comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
      comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
      comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
      comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

      OracleDataAdapter da = new OracleDataAdapter(comm);
      DataTable dt = new DataTable();
      da.Fill(dt);
      dataGridView1.DataSource = dt;


      Of course, you can also use OracleDataReader like above.



      Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.



      Regarding your actual error message, ensure the correct order:



      1. open the connection

      2. execute the command (or several commands)

      3. close the connection

      As OracleConnection implements IDisposal you should enclose it by using like this



      using (var conn = new OracleConnection() ) 
      conn.ConnectionString = "Data Source=...";
      conn.Open();
      // Code from above

      conn.Close();



      OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using






      share|improve this answer
























        up vote
        0
        down vote













        Your approach is not really the "direct path", I would do it like this. Either select the table directly:



        var s1 = "select email, collegename, cgpa, compname from student_cv 
        where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
        OracleCommand comm = new OracleCommand(s1, conn);
        comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
        comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
        comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";

        OracleDataAdapter da = new OracleDataAdapter(comm);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;


        Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:



        OracleDataReader dr = comm.ExecuteReader();
        while ( dr.Read() )
        // loop through the rows and process the rows one-by-one

        dr.close();


        The other way is to use a function or procedure like this:



        create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
        res SYS_REFCURSOR;
        BEGIN
        OPEN res FOR
        select email, collegename, cgpa, compname
        from student_cv
        where (cgpa >= x and yearsofexp >= y) and compname = z;

        RETURN res;
        end;


        And then call it in C# as this:



        comm.CommandType = CommandType.Text;
        comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
        comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
        comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
        comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
        comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

        OracleDataAdapter da = new OracleDataAdapter(comm);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;


        Of course, you can also use OracleDataReader like above.



        Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.



        Regarding your actual error message, ensure the correct order:



        1. open the connection

        2. execute the command (or several commands)

        3. close the connection

        As OracleConnection implements IDisposal you should enclose it by using like this



        using (var conn = new OracleConnection() ) 
        conn.ConnectionString = "Data Source=...";
        conn.Open();
        // Code from above

        conn.Close();



        OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using






        share|improve this answer






















          up vote
          0
          down vote










          up vote
          0
          down vote









          Your approach is not really the "direct path", I would do it like this. Either select the table directly:



          var s1 = "select email, collegename, cgpa, compname from student_cv 
          where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
          OracleCommand comm = new OracleCommand(s1, conn);
          comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
          comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
          comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";

          OracleDataAdapter da = new OracleDataAdapter(comm);
          DataTable dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;


          Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:



          OracleDataReader dr = comm.ExecuteReader();
          while ( dr.Read() )
          // loop through the rows and process the rows one-by-one

          dr.close();


          The other way is to use a function or procedure like this:



          create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
          res SYS_REFCURSOR;
          BEGIN
          OPEN res FOR
          select email, collegename, cgpa, compname
          from student_cv
          where (cgpa >= x and yearsofexp >= y) and compname = z;

          RETURN res;
          end;


          And then call it in C# as this:



          comm.CommandType = CommandType.Text;
          comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
          comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
          comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
          comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
          comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

          OracleDataAdapter da = new OracleDataAdapter(comm);
          DataTable dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;


          Of course, you can also use OracleDataReader like above.



          Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.



          Regarding your actual error message, ensure the correct order:



          1. open the connection

          2. execute the command (or several commands)

          3. close the connection

          As OracleConnection implements IDisposal you should enclose it by using like this



          using (var conn = new OracleConnection() ) 
          conn.ConnectionString = "Data Source=...";
          conn.Open();
          // Code from above

          conn.Close();



          OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using






          share|improve this answer












          Your approach is not really the "direct path", I would do it like this. Either select the table directly:



          var s1 = "select email, collegename, cgpa, compname from student_cv 
          where (cgpa >= :x and yearsofexp >= :y) and compname = :z";
          OracleCommand comm = new OracleCommand(s1, conn);
          comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
          comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
          comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";

          OracleDataAdapter da = new OracleDataAdapter(comm);
          DataTable dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;


          Instead OracleDataAdapter and OracleDataAdapter you can also use OracleDataReader:



          OracleDataReader dr = comm.ExecuteReader();
          while ( dr.Read() )
          // loop through the rows and process the rows one-by-one

          dr.close();


          The other way is to use a function or procedure like this:



          create or replace FUNCTION cv (x in int, y in int, z in varchar2) RETURN SYS_REFCURSOR as
          res SYS_REFCURSOR;
          BEGIN
          OPEN res FOR
          select email, collegename, cgpa, compname
          from student_cv
          where (cgpa >= x and yearsofexp >= y) and compname = z;

          RETURN res;
          end;


          And then call it in C# as this:



          comm.CommandType = CommandType.Text;
          comm.CommandText = "BEGIN :ret := cv(:x, :y, :z); END;";
          comm.Parameters.Add("x", OracleType.Number).Value = comboBox1.Text;
          comm.Parameters.Add("y", OracleType.Number).Value = comboBox2.Text;
          comm.Parameters.Add("z", OracleType.VarChar2).Value = "some text";
          comm.Parameters.Add("ret", OracleDbType.RefCursor, ParameterDirection.ReturnValue);

          OracleDataAdapter da = new OracleDataAdapter(comm);
          DataTable dt = new DataTable();
          da.Fill(dt);
          dataGridView1.DataSource = dt;


          Of course, you can also use OracleDataReader like above.



          Note, I don't have a computer in front of me for any testing, so please forgive me if I did one or two typos in the code examples.



          Regarding your actual error message, ensure the correct order:



          1. open the connection

          2. execute the command (or several commands)

          3. close the connection

          As OracleConnection implements IDisposal you should enclose it by using like this



          using (var conn = new OracleConnection() ) 
          conn.ConnectionString = "Data Source=...";
          conn.Open();
          // Code from above

          conn.Close();



          OracleDataReader and OracleDataAdapter also implements IDisposal, so is should be also enclosed by using







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered 2 days ago









          Wernfried Domscheit

          23k42757




          23k42757




















              up vote
              -4
              down vote













              You may need a commit; before end; ?






              share|improve this answer
























                up vote
                -4
                down vote













                You may need a commit; before end; ?






                share|improve this answer






















                  up vote
                  -4
                  down vote










                  up vote
                  -4
                  down vote









                  You may need a commit; before end; ?






                  share|improve this answer












                  You may need a commit; before end; ?







                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered 2 days ago









                  Ted at ORCL.Pro

                  61018




                  61018



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53229022%2fexecuting-procedure-from-visual-studio%23new-answer', 'question_page');

                      );

                      Post as a guest














































































                      這個網誌中的熱門文章

                      What does pagestruct do in Eviews?

                      Dutch intervention in Lombok and Karangasem

                      Channel Islands