Albrecht
Albrecht

Reputation: 47

Trouble inserting data into MySQL database (more than 200 characters) using a stored procedure

I have a small guest book form in asp/C# which works fine until I want to add more than 200 (I guess more than 255) characters into the message.

form and exception:
form and exception

The exception is: Data too long for column '_entry_message' at row 1

_entry_message is the parameter in my stored procedure. The data type in the db is longtext (I tried others as well).

My code in C# is:

protected void btnSend_Click(object sender, EventArgs e)
    {
        try
        {
            using (MySqlConnection sqlCon = new MySqlConnection(myConnectionString))
            {
                sqlCon.Open();

                MySqlCommand sqlCmd = new MySqlCommand("AddEntry", sqlCon);

                sqlCmd.CommandType = CommandType.StoredProcedure;

                sqlCmd.Parameters.AddWithValue("_entry_date", DateTime.Today);
                sqlCmd.Parameters.AddWithValue("_name", txtName.Text.Trim());
                sqlCmd.Parameters.AddWithValue("_e_mail", txtEmail.Text.Trim());
                sqlCmd.Parameters.AddWithValue("_entry_message", txtMessage.Text.Trim());
                
                sqlCmd.ExecuteNonQuery();

                GridFill();

                Clear();

                lblMessage.Text = success;
                lblMessage.ForeColor = System.Drawing.Color.Green;
                lblMessage.Visible = true;
            }
        }
        catch (Exception ex)
        {
            lblMessage.Text = error + ex.Message;
            lblMessage.ForeColor = System.Drawing.Color.Red;
            lblMessage.Visible = true;
        }
    }

I don't think the code is the problem. I guess something is happening during the stored procedure:

CREATE DEFINER=`albrecht`@`%` PROCEDURE `AddEntry`(_entry_date datetime,_name varchar(45),_e_mail varchar(45),_entry_message varchar(250)) BEGIN insert into tbl_guestbook(entry_date,name,e_mail,entry_message) values(_entry_date,_name,_e_mail,_entry_message); END

Upvotes: 1

Views: 148

Answers (1)

slaakso
slaakso

Reputation: 9070

The datatype for parameter _entry_message is varchar(250). If the datatype for column tbl_guestbook.entry_message is longtext, the datatype for _entry_message should match that.

Upvotes: 1

Related Questions