Announcement

Collapse
No announcement yet.

Command.Parameters

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • Command.Parameters

    Hi,
    I'm a new here and would like some help with my code please!!
    I'm using VS.Net 2003 and connect to MySQL 5.1. The problem I have is passing a value from a Control 'TB_Vorname' to a Command.Parameter 'f_name'. The server keeps recieving NULL instead of the value. Cann anybody let me know where my code in going wrong.

    Thanks

    Wayne




    //Connection to MySQL server
    OdbcConnection cn = new OdbcConnection(MyConString);

    //Create Odbc command object
    OdbcCommand cmd = new OdbcCommand();

    cmd.CommandType = CommandType.StoredProcedure;

    cmd.Parameters.Add("@f_name", OdbcType.VarChar, 75).Value = TB_Vorname.Text;
    cmd.Parameters.Add("@l_name", OdbcType.VarChar, 75).Value = TB_Nachname.Text;

    //Set the Select statement in the CommandText property and set the
    //connection property to the "cn" Connection object.
    cmd.CommandText = "Insert into name (date_added, date_modified, f_name, l_name)values (now(), now(), @f_name, @l_name)";
    cmd.Connection = cn;

    try
    {
    cn.Open();
    cmd.ExecuteNonQuery();
    }
    catch (Exception ex)
    {
    MessageBox.Show(ex.Message);
    }
    finally
    {
    if (cn.State == ConnectionState.Open)
    {
    cn.Close();
    }
    }

  • #2
    Hi,

    I don't have a complete solution, but some hints.

    First, you should use a DbProvider that is specialized and optimized for MySql. Take a look at ConnectionStrings MySql. The best (or mostly used) NET-Provider may be the one of CoreLabs.

    Second, you set the DbCommand.CommandType as StoredProcedure, but use it as Text. In your situation, you should it stay on the standard value "Text".

    Last, the DbParameters are set by "@" or ":" or "?" with or without fieldname. Each DbProvider or DBMS has its own preference. You must look for the correct dealing of the DbProvider.

    Juergen

    Comment


    • #3
      still not working

      Hi Juergen,
      have just worked through your tipps (thanks) but still the same problem.
      The update does work when I use values but not when I try and use the controls. The controls do work for reading, have you maybe another idea.

      Thanks again

      Wayne

      Comment


      • #4
        Hello

        MySql Uses "?" instead of "@" for Parameters.
        Gruss

        Mirko

        Mappen statt hacken mit dem .NET O/R Mapper Invist

        Comment

        Working...
        X