?
Solved

Execute stored procedure in mysql database

Posted on 2016-09-07
2
Medium Priority
?
48 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
[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
2 Comments
 
LVL 30

Accepted Solution

by:
Olaf Doschke earned 2000 total points
ID: 41788469
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
ID: 41788944
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

Windows Server 2016: All you need to know

Learn about Hyper-V features that increase functionality and usability of Microsoft Windows Server 2016. Also, throughout this eBook, you’ll find some basic PowerShell examples that will help you leverage the scripts in your environments!

Question has a verified solution.

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

Real-time is more about the business, not the technology. In day-to-day life, to make real-time decisions like buying or investing, business needs the latest information(e.g. Gold Rate/Stock Rate). Unlike traditional days, you need not wait for a fe…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…
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…
Suggested Courses

719 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