Avatar of mjhoagland
mjhoagland

asked on 

Mysql Out Parameters Don't Return Data

Hello,

When the method workerAgentContactInfo_DoWork is ran is executes fine.  All the data I need to be returned is returned including the Out parameter in the mysqlComm object.  However, when the method GetContactInformation is ran the Out parameters don't have data in them.  I have ran its Stored Procedure in Query Browser and it returns information as I expect.  I read of a bug where ExecuteNonQuery() would fill Out params but ExecuteReader() wouldn't.  I have attempted this fix and it didn't work.

In both cases the command is set to a Stored Procedure.  The parameters are set as they need to be as far as input/output is concerened.  What you see below as the parameters being Input/Output was a desperation measure trying to do a catch-all on the parameters but that didn't work either.  What could be wrong?

Thanks.
private void workerAgentContactInfo_DoWork(object sender, DoWorkEventArgs e)
        {
            DataTable agentsContacts = new DataTable();
            agentsContacts.Columns.Add("Homepage", typeof(string));
            agentsContacts.Columns.Add("ContactName", typeof(string));
            agentsContacts.Columns.Add("ContactDepartment", typeof(string));
            agentsContacts.Columns.Add("ContactType", typeof(string));
            agentsContacts.Columns.Add("ContactInfo", typeof(string));
 
            MySqlCommand mysqlComm = new MySqlCommand("tiger_getAgentContacts");
            mysqlComm.CommandType = CommandType.StoredProcedure;
            mysqlComm.Connection = mysqlConn;
 
            MySqlParameter param = new MySqlParameter("_agentId", MySqlDbType.Int32);
            param.Direction = ParameterDirection.Input;
            param.Value = agentId;
 
            mysqlComm.Parameters.Add(param);
 
            param = new MySqlParameter("_homepage", MySqlDbType.String);
            param.Direction = ParameterDirection.Output;
 
            mysqlComm.Parameters.Add(param);
 
            MySqlDataAdapter adapter = new MySqlDataAdapter(mysqlComm);
 
            try
            {
                adapter.Fill(agentsContacts);
            }
            catch (Exception ex)
            {
                switch (MessageBox.Show(ex.Message, "Database Error", MessageBoxButtons.OKCancel))
                {
                    case DialogResult.OK:
                        // do nothing
                        break;
                    case DialogResult.Cancel:
                        Application.Exit();
                        break;
                }
            }
            finally
            {
                if (mysqlConn.State != ConnectionState.Closed)
                    mysqlConn.Close();
            }
 
            TabPage page = tabControlCalls.TabPages["homeTab"];
 
            if (agentsContacts.Rows.Count > 0)
            {
                FillContactsPanel(page, agentsContacts);
            }
 
            homeTabUrlBase = mysqlComm.Parameters["_homepage"].Value.ToString();
            homeTabUrlBase = homeTabUrlBase.Replace("[AGENTID]", agentId);
 
            if (homeTabUrlBase.Length == 0)
                homeTabUrlBase = "About:No Web Information is Available for Agent " + agentId;
 
            NavigatePanelBrowser(page, homeTabUrlBase);
        }
 
private void GetContactInformation()
        {
            try
            {
                TabPage page = (TabPage)this.Invoke(new DelegateTabPageString(GetTabPage), new object[] { pageNameInfo });
                Label lblCallerId = (Label)page.Controls.Find("CallerIdName", false)[0];
 
                int indexOfColon = lblCallerId.Text.IndexOf(":");
 
                bool hasId = false;
 
                if (indexOfColon != -1)
                    hasId = true;
 
                if (hasId)
                {
                    string identifier = lblCallerId.Text.Substring(0, indexOfColon);
 
                    dtContacts.Rows.Clear();
 
                    MySqlCommand mysqlComm = new MySqlCommand("tiger_getCallerContacts");
                    mysqlComm.CommandType = CommandType.StoredProcedure;
                    mysqlComm.Connection = mysqlConn;
 
                    MySqlParameter param = new MySqlParameter("_asteriskId", MySqlDbType.String);
                    param.Direction = ParameterDirection.InputOutput;
                    param.Value = identifier;
 
                    mysqlComm.Parameters.Add(param);
 
                    param = new MySqlParameter("_companyId", MySqlDbType.Int32);
                    param.Direction = ParameterDirection.InputOutput;
 
                    mysqlComm.Parameters.Add(param);
 
                    param = new MySqlParameter("_companyName", MySqlDbType.String);
                    param.Direction = ParameterDirection.InputOutput;
 
                    mysqlComm.Parameters.Add(param);
 
                    param = new MySqlParameter("_webpopurl", MySqlDbType.String);
                    param.Direction = ParameterDirection.InputOutput;
 
                    mysqlComm.Parameters.Add(param);
 
                    param = new MySqlParameter("_callinnumber", MySqlDbType.Int32);
                    param.Direction = ParameterDirection.InputOutput;
 
                    mysqlComm.Parameters.Add(param);
 
                    MySqlDataAdapter adapter = new MySqlDataAdapter(mysqlComm);
 
                    try
                    {
                        adapter.Fill(dtContacts);
                    }
                    catch (Exception ex)
                    {
                        ErrorReporting.Reporter.DumpError(ex);
                    }
                    finally
                    {
                        if (mysqlConn.State != ConnectionState.Closed)
                            mysqlConn.Close();
                    }
 
                    string url = mysqlComm.Parameters["_webpopurl"].Value.ToString();
                    url = url.Replace("[AGENTID]", agentId);
                    url = url.Replace("[CALLERIDNUM]", identifier);
                    url = url.Replace("[COMPANYCODE]", identifier);
 
                    if (url.Length == 0)
                        url = "About:No page for the identifier, '" + identifier + "', was found";
 
                    this.Invoke(new DelegateVoidTabPageString(NavigatePanelBrowser), new object[] { page, url });
 
                    string callNumber = mysqlComm.Parameters["_callinnumber"].Value.ToString();
 
                    this.Invoke(new DelegateVoidTabPageString(AppendCallInNumberToTab), new object[] { page, callNumber });
 
                    for (int i = 0; i < dtContacts.Rows.Count; i++)
                    {
                        dtContacts.Rows[i]["CompanyName"] = mysqlComm.Parameters["_companyName"].Value.ToString();
                        dtContacts.Rows[i]["WebPopUrl"] = mysqlComm.Parameters["_webpopurl"].Value.ToString();
                    }
 
                    if (dtContacts.Rows.Count > 0)
                    {
                        this.Invoke(new DelegateVoidTabPageDataTable(FillContactsPanel), new object[] { page, dtContacts });
                        txtForward.Clear();
                    }
                }
                else
                {
                    this.Invoke(new DelegateVoidTabPageString(NavigatePanelBrowser), 
                        new object[] { page, "About:Company Identifier Not Found" });
                }
            }
            catch (Exception ex)
            {
                ErrorReporting.Reporter.DumpError(ex);
            }
        }

Open in new window

.NET ProgrammingMySQL Server

Avatar of undefined
Last Comment
mjhoagland
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

can you please show the MySQL procedure.
Avatar of mjhoagland
mjhoagland

ASKER

Here are both.  The first is the one used by the _DoWork method and the second one below it is the SPROC for the GetContactInformation method.

Thanks!
CREATE DEFINER=`tiger`@`%` PROCEDURE `tiger_getAgentContacts`(IN _agentId int, OUT _homepage varchar(100))
BEGIN
      SET _homepage = (SELECT a.homepage FROM Agents a WHERE a.AgentId=_agentId);
      (
        SELECT c.name, c.department, cd.detailType, cd.detail
        FROM Agents a, ContactIdentities ci, Contacts c, ContactDetails cd
        WHERE a.ContactIdentifier = ci.ContactIdentifier AND
        ci.ContactId=c.contactId AND c.contactId=cd.contactId
        AND a.AgentId=_agentId
      )
      UNION
      (
        SELECT c.name, c.department, cd.detailType, cd.detail
        FROM Agents a, AgentGroups ag, Groups g, ContactIdentities ci, Contacts c, ContactDetails cd
        WHERE a.AgentId=ag.AgentId AND g.GroupId=ag.GroupId AND
        g.ContactIdentifier = ci.ContactIdentifier AND
        ci.ContactId=c.contactId AND c.contactId=cd.contactId
        AND a.AgentId=_agentId
      );
END
 
CREATE DEFINER=`tiger`@`%` PROCEDURE `tiger_getCallerContacts`(IN _asteriskId varchar(25),OUT _companyId int,OUT _companyName varchar(50),OUT _webpopurl varchar(100),OUT _callinnumber int)
BEGIN
       DECLARE compId int;
       SELECT c.CompanyId AS _companyId, c.name AS _companyName, c.webpopurl AS _webpopurl, c.callinnumber AS _callinnumber
       FROM Companies c
       WHERE c.asteriskId=_asteriskId;
 
       SELECT c2.name, c2.department,cd.detailType, cd.detail
       FROM Companies c, ContactIdentities ci, Contacts c2, ContactDetails cd
       WHERE c.ContactIdentifier = ci.ContactIdentifier AND
       ci.ContactId=c2.ContactId AND c2.ContactId=cd.ContactId
       AND c.asteriskId=_asteriskId;
END

Open in new window

the problem is that your second procedure does not "fill" the parameters

CREATE DEFINER=`tiger`@`%` PROCEDURE `tiger_getCallerContacts`(IN _asteriskId varchar(25),OUT _companyId int,OUT _companyName varchar(50),OUT _webpopurl varchar(100),OUT _callinnumber int)
BEGIN
       DECLARE compId int;
       SELECT c.CompanyId , c.name , c.webpopurl , c.callinnumber 
       INTO  _companyId, _companyName, _webpopurl, _callinnumber
       FROM Companies c
       WHERE c.asteriskId=_asteriskId;
 
       SELECT c2.name, c2.department,cd.detailType, cd.detail
       FROM Companies c, ContactIdentities ci, Contacts c2, ContactDetails cd
       WHERE c.ContactIdentifier = ci.ContactIdentifier AND
       ci.ContactId=c2.ContactId AND c2.ContactId=cd.ContactId
       AND c.asteriskId=_asteriskId;
END

Open in new window

Avatar of mjhoagland
mjhoagland

ASKER

And the reason the tiger_getAgentContacts proc works because of the UNION statement?
ASKER CERTIFIED SOLUTION
Avatar of Guy Hengel [angelIII / a3]
Guy Hengel [angelIII / a3]
Flag of Luxembourg image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of mjhoagland
mjhoagland

ASKER

Thank you very much.
.NET Programming
.NET Programming

The .NET Framework is not specific to any one programming language; rather, it includes a library of functions that allows developers to rapidly build applications. Several supported languages include C#, VB.NET, C++ or ASP.NET.

137K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo