Solved

Execute stored procedure in mysql database

Posted on 2016-09-07
2
32 Views
Last Modified: 2016-09-07
I have setup a mysql database that successfully connects and populates datagrids in my VS2015-c# application under development.
I have created a stored procedure:

CREATE DEFINER=`glsinger684`@`%` PROCEDURE `create_co_record`(IN `PrincipalID` VARCHAR(50))
      LANGUAGE SQL
      NOT DETERMINISTIC
      CONTAINS SQL
      SQL SECURITY DEFINER
      COMMENT 'Creates new company record with PrincipalID'
BEGIN
insert into company_info (PrincipalID)
values (PrincipalID);
END

I can successfully execute the stored procedure from a query using the management utility.
Next to test from my asp.net app I have the following code linked to a button:

using System;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Data;
using System.Configuration;
using Microsoft.AspNet.Identity;
using Microsoft.AspNet.Identity.Owin;
using Owin;
using System.Web.UI.WebControls;
using MySql.Data.MySqlClient;

namespace EstimatingSheet_080916.Account
>
>
>
     protected void btnAddCo_Click(object sender, EventArgs e)
        {
            string conn = "server=mysqlcluster8.registeredsite.com;user id=glsinger684;persistsecurityinfo=True;password=<mypassword>;database=estimating";
            MySqlConnection sql_conn = new MySqlConnection(conn);

            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = sql_conn;
            sql_conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "CALL create_co_record (PrincipalID)";          
            cmd.Parameters.AddWithValue("PrincipalID", "TESTTEST");
            cmd.Parameters["PrincipalID"].Direction = ParameterDirection.Input;

When I run the app and hit the button nothing appears to happen.  I get no errors, but the desired effect of the SP does not occur.  Since the datagrids populate I don't see why this would be a permissions issue, but in any event any research on granting permissions that I have tried does not work.
Any suggestions would be much appreciated.
0
Comment
Question by:GregorySinger
2 Comments
 
LVL 29

Accepted Solution

by:
Olaf Doschke earned 500 total points
Comment Utility
Is that all code? Where is cmd.ExecuteNonQuery()? You prepare everything, but don't execute, unless you just didn't posted that line. It's explains in a very simple way, why nothing would happen, though.

I think you also need a syntax with @param, @ prefixed to variable names...

...
            MySqlCommand cmd = new MySqlCommand();
            cmd.Connection = sql_conn;
            sql_conn.Open();
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.CommandText = "CALL create_co_record (@PrincipalID)";           
            cmd.Parameters.AddWithValue("@PrincipalID", "TESTTEST");
            cmd.Parameters["@PrincipalID"].Direction = ParameterDirection.Input;
            cmd.ExecuteNonQuery();

Open in new window


Bye, Olaf.
0
 

Author Closing Comment

by:GregorySinger
Comment Utility
Olaf,
You hit the nail on the head.  After a few minor modifications to other code lines, it worked just fine.
Thanks again!

Greg
0

Featured Post

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Does the idea of dealing with bits scare or confuse you? Does it seem like a waste of time in an age where we all have terabytes of storage? If so, you're missing out on one of the core tools in every professional programmer's toolbox. Learn how to …
Creating and Managing Databases with phpMyAdmin in cPanel.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
Get a first impression of how PRTG looks and learn how it works.   This video is a short introduction to PRTG, as an initial overview or as a quick start for new PRTG users.

744 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now