1

I was creating a program that can insert audio into mysql database(i am using mediumblob data type) but when i click insert button it showed an error "column data cannot be null". Here is my code :

private void buttonBrowse_Click(object sender, EventArgs e)
    {
        OpenFileDialog ofd = new OpenFileDialog();
        ofd.Filter = "WAV Audio Files|*.wav|M4A Audio Files|*.m4a";
        if (ofd.ShowDialog() == DialogResult.OK)
        {
            textBoxBrowse.Text = ofd.FileName;
        }
    }

    private void buttonSimpan_Click(object sender, EventArgs e)
    {
        OdbcConnection con = new OdbcConnection(Program.konek);
        OdbcCommand com = new OdbcCommand("insert into audio(data) values(@voice)", con);
        byte[] stream = File.ReadAllBytes(@textBoxBrowse.Text);
        MessageBox.Show(stream.ToString());
        if (stream.Length > 0)
        {
            com.Parameters.AddWithValue("@voice", stream);
            con.Open();
            int result = com.ExecuteNonQuery();
            if (result > 0)
                MessageBox.Show("insert done");
            con.Close();
        }
    }

Does anyone have a solution for this ?

1 Answers1

0

From MSDN OdbcParameter Class

When CommandType is set to Text, the .NET Framework Data Provider for ODBC does not support passing named parameters to an SQL statement or to a stored procedure called by an OdbcCommand. In either of these cases, use the question mark (?) placeholder. For example:

So change your query

OdbcCommand com = new OdbcCommand("insert into audio(data) values(?)", con);
byte[] stream = File.ReadAllBytes(@textBoxBrowse.Text);
    MessageBox.Show(stream.ToString());
    if (stream.Length > 0)
    {
        com.Parameters.AddWithValue("voice", stream);
        con.Open();
        int result = com.ExecuteNonQuery();
        if (result > 0)
            MessageBox.Show("insert done");
        con.Close();
    }
}
Nagaraj S
  • 13,316
  • 6
  • 32
  • 53