Click here to Skip to main content
15,890,438 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
Hello, I am new to C# and MySQL. I am trying to add data to a database and I get the following error.

System.Collections.Generic.KeyNotFoundException {"The given key was not present in the dictionary."}
Error is on the following line.

connection.Execute("splonglat_insert", p, commandType: CommandType.StoredProcedure);

I can add data to the database in MySQL.

C#
using Dapper;
using LonglatLibrary.DataAccess;
using System;
using System.Collections.Generic;
using System.Data;
using System.Text;
namespace LonglatLibrary
{
    //public class MySQLConnector : IDataConnection
      public class MySqlConnection : IDataConnection
    {
        public Longlatmodel CreateLonglat(Longlatmodel model)
        {
            using (IDbConnection connection = new MySql.Data.MySqlClient.MySqlConnection(GlobalConfig.CnnString("spead_im_testing")))
            {
                var p = new DynamicParameters();
                p.Add("RailSiteCity", model.RailSiteCity);
                p.Add("RailSiteState", model.RailSiteState);
                p.Add("SiteLongitude", model.SiteLongitude);
                p.Add("SiteLatitude", model.SiteLatitude);
                p.Add("SiteLongitudeDirection", model.SiteLongitudeDirection);
                p.Add("SiteLatitudeDirection", model.SiteLatidudeDirection);
                p.Add("SiteCity", model.SiteCity);
                p.Add("SiteState", model.SiteState);

                connection.Execute("splonglat_insert", p, commandType: CommandType.StoredProcedure);
                return model;
            }
        }
    }
}



Below is my stored procedure. What am I missing?
CREATE DEFINER=`root`@`localhost` PROCEDURE `splonglat_insert`(in RailSiteCity varchar(35),
in RailSiteState varchar(2),
in SiteLongitude varchar(7),
in SiteLatitude varchar(7),
in SiteLongitudeDirection varchar(1),
in SiteLatitudeDirection varchar(1),
in SiteCity varchar(35),
in SiteState varchar(2))
BEGIN
insert into Longlat(RailSiteCity, RailSiteState, SiteLongitude, SiteLatitude, SiteLongitudeDirection, SiteLatitudeDirection, SiteCity, SiteState)
values(RailSiteCity, RailSiteState, SiteLongitude, SiteLatitude, SiteLongitudeDirection, SiteLatitudeDirection, SiteCity, SiteState);
END


What I have tried:

Not sure what to try. I am thinking I am missing something on the connection.Execute line but can't figure it out.
Posted
Updated 11-Sep-18 5:22am
v3
Comments
Richard Deeming 11-Sep-18 11:38am    
From what I can see, the MySqlConnection class[^] doesn't contain a method called Execute. Is that a custom extension method?
j snooze 11-Sep-18 17:22pm    
Is something wrong with this line, or is the model property actually misspelled? (Latidude?)
p.Add("SiteLatitudeDirection", model.SiteLatidudeDirection);
MIDCOPC 11-Sep-18 17:52pm    
I have fixed the spelling errors. The windows form will come up, I can enter data and click update. Then I get the error. System.Collections.Generic.KeyNotFoundException {"The given key was not present in the dictionary."}
MIDCOPC 14-Sep-18 11:28am    
@Richard, I don't think it is a custom extension. I am new at this. I will look and see if I can add to the MySqlConnection class. Thank you
MIDCOPC 24-Sep-18 17:31pm    
Update: I am using Dapper and MySQL. I have been over the code and fixed all of the spelling errors. In MySQL I can see the connection to the database but still get the error when I click the update button.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900