views:

32

answers:

3

Hello,

from within a C# WinForms-App I must execute a parameterized Stored Procedure on a MS SQL Express Server. The Database Connection works, the Procedure works either, but I get an Error Message:

42000: Missing Parameter '@KundenEmail'

although I'm sure I added the parameter correctly. Maybe some of you could have a look - I don't know what to search for any more...

OdbcConnection ODBCConnection = new OdbcConnection();

try
{
    ODBCConnection.ConnectionString = ODBCConnectionString;
    ODBCConnection.Open();
}
catch (Exception DatabaseConnectionEx)
{
    if (ODBCConnection != null)
        ODBCConnection.Dispose();

    // Error Message

    return null;
}

OdbcParameter ODBCParameter = new OdbcParameter("@KundenEmail", OdbcType.NChar, 50);
ODBCParameter.Value = KundenEmail;

OdbcCommand ODBCCommand = new OdbcCommand("getDetailsFromEmail", ODBCConnection);
ODBCCommand.CommandType = CommandType.StoredProcedure;
ODBCCommand.Parameters.Add(ODBCParameter);

DataTable DataTable = new DataTable();

OdbcDataAdapter ODBCDatadapter = new OdbcDataAdapter(ODBCCommand);
ODBCDatadapter.Fill(DataTable);
ODBCDatadapter.Dispose();

ODBCConnection.Close();
ODBCConnection.Dispose();

This is the error message I get:

ERROR [4200][Microsoft][ODBC SQL Server]The Procedure or method 'getDetailsFromEmail' expects the '@KundenEmail'-parameter, which was not supplied.

Ah, I missed the connection string

private static String ODBCConnectionString = "Driver={SQL Server};Server=TESTSRV\\SQLEXPRESS;Database=TestDatabase;";

Any ideas? Thanks in advance, Daniel

A: 

Don't use ODBCConnection to connect to a SQL Server. Use the "normal" SqlConnection, SqlCommand, etc. These are the ones made to work with SQL Server.

EDIT: Also, you should use the SqlConnectionStringBuilder to assembly the connection string. This is far less error prone than putting the entire connection string into a configuration file or creating it by hand.

Thorsten Dittmar
Hello Thorsten,thanks for your reply. I added the OdbcConnectionStringBuilder - I'm not able to use the direct SQL connection because I am told to use the ODBC Connection.For testing purpose I used the Sql-things before (because I already used it) and then switched it to ODBC. Somehow I'm not able to make it work...
dhh
How about trying `ODBCCommand.Parameters.AddWithValue(...)`? Does that exist for OdbcCommands? The requirement of using the ODBC connection doesn't make sense if you connect to the SQL Server directly. Just out of curiousity: does it work when you use the respective `Sql...` classes?
Thorsten Dittmar
Yes, indeed it works when I use `Sql...`-Classes.Well, the ODBC Connection is to be used, because it is not sure which database system will be used.
dhh
A: 

Well - I now managed to solve the problem on my own, with some help from the MSDN-documentation.

The correct statement to execute a stored procedure via ODBC is as follows:

OdbcCommand ODBCCommand = new OdbcCommand("{call getDetailsFromEmail (?)}", ODBCConnection);
ODBCCommand.CommandType = CommandType.StoredProcedure;
ODBCCommand.Parameters.AddWithValue("@KundenEmail", KundenEmail);

Nevertheless - thanks for your help Thorsten.

dhh
Ah! Didn't know that. See? You learn something new every day...
Thorsten Dittmar
So don't forget to accept one of the answer - your one or somebody else's
abatishchev
dhh
+1  A: 

Anyway it's better your code would look like this:

using (OdbcConnection connection = new OdbcConnection(connectionString))
using (OdbcCommand command = connection.CreateCommand())
{
    command.CommandText = commandText;
    command.CommandType = CommandType.StoredProcedure;

    command.Paremeters.Add("@KundenEmail", OdbcType.NChar, 50).Value = KundenEmail

    DataTable dataTable = new DataTable();

    connection.Open();    
    using (OdbcDataAdapter adapter = new OdbcDataAdapter(command))
    {
        adapter.Fill(dataTable);
    }
}

But rather better to use SqlConnection/SqlCommand/SqlDataAdapter instead of ODBC types. Syntax still the same

abatishchev