Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

How to execute stored procedures on mysql from C#?

Posted on 2009-04-13
16
Medium Priority
?
1,168 Views
Last Modified: 2013-12-17
{"ERROR [HYT00] Incorrect number of arguments for PROCEDURE test expected 5, got 0"}      System.Data.Odbc.OdbcException
Trying to insert form data into a table thru an sp after the click of a button..

             
OdbcCom.CommandType = System.Data.CommandType.StoredProcedure;
                OdbcParameter pname = new OdbcParameter();
                pname.Value = txtbox_Name.Text;
                pname.Direction = System.Data.ParameterDirection.Input;
                OdbcParameter padd1 = new OdbcParameter();
                padd1.Direction = System.Data.ParameterDirection.Input;
                padd1.Value = txtbox_Add1.Text;
                OdbcParameter padd2 = new OdbcParameter();
                padd2.Direction = System.Data.ParameterDirection.Input;
                padd2.Value = txtbox_Add2.Text;
                OdbcParameter pstate = new OdbcParameter();
                pstate.Direction = System.Data.ParameterDirection.Input;
                pstate.Value = txtbox_St.Text;
                OdbcParameter pzip = new OdbcParameter();
                pzip.Direction = System.Data.ParameterDirection.Input;
                pzip.Value = txtbox_Zip.Text;
                OdbcCom.Parameters.Add(pname);
                OdbcCom.Parameters.Add(padd1);
                OdbcCom.Parameters.Add(padd2);
                OdbcCom.Parameters.Add(pstate);
                OdbcCom.Parameters.Add(pzip);
 
OdbcCon.Open();
                int i = OdbcCom.ExecuteNonQuery();
                OdbcCon.Close();

Open in new window

0
Comment
Question by:Pavithra_S
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 6
  • 6
  • 3
  • +1
16 Comments
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24134123
Could you post the details on your stored procedure, please?
0
 

Author Comment

by:Pavithra_S
ID: 24134209
Thanks a lot for your reply!
I just got rid of the error but now I am facing an issue with the invalid cast exception for
int i = OdbcCom.ExecuteNonQuery();

Any ideas?

Previously I was callinf the Stored procedure without parameters.. I was adding the parameters but failed to call the sp with it...

0
 
LVL 9

Accepted Solution

by:
Sreedhar Vengala earned 660 total points
ID: 24134445
I got a sample here
var cn = new OdbcConnection { ConnectionString = (@"Driver={SQL Server};Server=localhost\sql2008;Database=MtArthur_440;Trusted_Connection=Yes") };
            cn.Open();
            var cmd = new OdbcCommand("{ CALL pr_GetTranCategoryID(?,?) }", cn) { CommandType = CommandType.StoredProcedure };
            var tranCategory = new OdbcParameter("@tran", OdbcType.NVarChar) { Direction = ParameterDirection.Input };
            var catID = new OdbcParameter("@tranID", OdbcType.Int) { Direction = ParameterDirection.Output };
            cmd.Parameters.Add(tranCategory);
            cmd.Parameters.Add(catID);
           
            tranCategory.Value = "Modular Transaction";
            catID.Value = 0;
            int result = cmd.ExecuteNonQuery();
            cn.Close();

other thing : what is you output parameter in SP (is of type int ?)
0
Nothing ever in the clear!

This technical paper will help you implement VMware’s VM encryption as well as implement Veeam encryption which together will achieve the nothing ever in the clear goal. If a bad guy steals VMs, backups or traffic they get nothing.

 
LVL 18

Assisted Solution

by:philipjonathan
philipjonathan earned 60 total points
ID: 24134495
Will this help?
int i = (int) OdbcCom.ExecuteScalar();
0
 
LVL 9

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 660 total points
ID: 24134677
Can you show your Stored Proc. ?
0
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24134698
Looks like you're trying to implicitly convert to an int and therefore you get the error.

Here is more info on when to use ExecuteNonQuery() vs the other options:

http://blogs.x2line.com/al/archive/2007/05/01/3049.aspx
0
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24134700
I meant to say: you are implicitly converting an object to an int.
0
 

Author Comment

by:Pavithra_S
ID: 24141906
I have a zipcode column which was int,
and in my code  pzip.Value = txtbox_Zip.Text;
I was trying to convert this to int before calling the stored proc, which was throwing the invalid cast exception in my guess,
Now I have changed this column to varchar and savign this as text...
How do you pass an integer value from a textbox to a stored procedure? In my guess this is quite simple but I am doing something wrong... is there any property that I need to set??
sreeven
I will try the sample u have given and will let u knw..
newbieal
Thanks for the link I wanted to know more about executenonquery

phlipjonathan
I need to use executequery... I usually use executescalar when I am sql statement directly without using the commands..
0
 
LVL 9

Assisted Solution

by:Sreedhar Vengala
Sreedhar Vengala earned 660 total points
ID: 24143148
if textbox value = "134"
var catID = new OdbcParameter("@tranID", OdbcType.Int) { Direction = ParameterDirection.Output };
cmd.Parameters.Add(catID);
catID.Value = Convert.Int32(textbox1.text);
0
 

Author Comment

by:Pavithra_S
ID: 24143369
DELIMITER $$

DROP PROCEDURE IF EXISTS `sp_insertcompany` $$
CREATE DEFINER=`srguser`@`%` PROCEDURE `test`(IN name varchar(30),IN add1 varchar(50), IN add2 varchar(20),IN state varchar(15), IN zip varchar(10),OUT last_inserted_ID INT)
BEGIN
Insert into company (Name, Address1, Address2, State, Zip) values ('name', 'add1', 'add2','state','zip');
select @@IDENTITY as 'last_inserted_id';
END $$

DELIMITER ;
The above is my Sp on mysql
I am able to create  it but when I cal this procedure from C# its giving the
ERROR HYT00 "Can't return the result set in the given context"

Can u help me with this?? Thanks for all the help! really appreciate it
0
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24152946
I've found this info that might help explain it:
For statements that can be determined only at runtime to return a result set, a PROCEDURE %s can't return a result set in the given context error occurs (ER_SP_BADSELECT).
Source: http://dev.mysql.com/doc/refman/5.0/en/create-procedure.html
0
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24152957
0
 

Author Comment

by:Pavithra_S
ID: 24161527
DELIMITER $$

DROP PROCEDURE IF EXISTS `gps_srg`.`sp_insertcustomer` $$
CREATE PROCEDURE `gps_srg`.`sp_insertcustomer` (IN compid INT(11),IN fname VARCHAR(20),IN lname VARCHAR(20),IN phone VARCHAR(10),IN fax VARCHAR(10),IN email VARCHAR(100))
BEGIN
INSERT INTO customer (CompanyID,FirstName,LastName,Phone,Fax,email) values ('compid','fname','lname','phone','fax','email')
END $$

DELIMITER ;
Some one Please tell me what is wrong with this statement!! its throwing some syntax error on line 4
Thanks a lot!
0
 

Author Comment

by:Pavithra_S
ID: 24163204
regarding the
ERROR HYT00 "Can't return the result set in the given context"
I found an article which uses transactions in C#.. apparently we need to use transactions in C# for multiple sql statements...
http://msdn.microsoft.com/en-us/library/system.data.odbc.odbctransaction.aspx

That worked for me! Thanks for all the answers,
For people using C# and Mysql this might be really important....
0
 
LVL 4

Assisted Solution

by:newbieal
newbieal earned 780 total points
ID: 24164132
Pavithra,

Glad to hear it.  Could you please close out your question then?  Thanks!
0
 

Author Closing Comment

by:Pavithra_S
ID: 31571277
I still havent found a complete solution, i am still having trouble inserting the integer into the mysql table.. will open as a different question bu giving a more precise picture of the problem,, thanks!!
0

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article aims to explain the working of CircularLogArchiver. This tool was designed to solve the buildup of log file in cases where systems do not support circular logging or where circular logging is not enabled
Backups and Disaster RecoveryIn this post, we’ll look at strategies for backups and disaster recovery.
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question