1

i have question. I want to store data on my mysqldatabase using this C#

private void btnSaveFilm_Click(object sender, EventArgs e)
    {
        try 
        {
            MySqlConnection conn = new MySqlConnection(connection.mysqlconnectionbuilder());
            conn.Open();
            MySqlCommand cmd = conn.CreateCommand();
            cmd.CommandText = "INSERT INTO film(judul,genre,asal,kondisi)"
                + "VALUES(@judul,@genre,@asal,@kondisi)";
            cmd.CommandType = CommandType.Text;
            cmd.Parameters.AddWithValue("@judul", textBoxJudul.Text);
            cmd.Parameters.AddWithValue("@genre", category(comboBoxGenre.SelectedValue.ToString()).ToString());
            cmd.Parameters.AddWithValue("@asal", asal(comboBoxAsal.SelectedValue.ToString()).ToString());
            cmd.Parameters.AddWithValue("@kondisi", checkedStatus());
            cmd.ExecuteNonQuery();
            conn.Close();
        }
        catch(Exception exe) 
        { 
            Console.Write("Error on Save Film : " + exe.ToString() + "\n" +exe.Message);
        }

    }

but it shows error System.NullReferenceException: Object reference not set to an instance of an object.

Error at this line 40:

   cmd.Parameters.AddWithValue("@genre",kategori(comboBoxGenre.SelectedValue.ToString()).ToString());

how to solve that?

8
  • 2
    On which line is this exception being thrown? Commented Dec 31, 2012 at 5:31
  • @MadSkunk line 40. the script regarding the method is here: stackoverflow.com/questions/14097646/… Commented Dec 31, 2012 at 5:33
  • Superb :) Where is the line number 40 in your code ? Add the line number40 in ur code @randytan Commented Dec 31, 2012 at 5:34
  • How can we know that which is line 40..?? Commented Dec 31, 2012 at 5:34
  • @theunlucky this one : cmd.Parameters.AddWithValue("@genre", kategori(comboBoxGenre.SelectedValue.ToString()).ToString()); Commented Dec 31, 2012 at 5:35

2 Answers 2

5

refractor your code into this, use using statement,

string connString = connectionmysqlconnectionbuilder();
using (MySqlConnection conn = new MySqlConnection(connString)
{
    using (MySqlCommand cmd = new MySqlCommand())
    {
        cmd.Connection = conn;
        cmd.CommandText = @"INSERT INTO film(judul,genre,asal,kondisi) 
                             VALUES(@judul,@genre,@asal,@kondisi)";
        cmd.Parameters.AddWithValue("@judul", textBoxJudul.Text);
        cmd.Parameters.AddWithValue("@genre", kategori(comboBoxGenre.Text).ToString());
        cmd.Parameters.AddWithValue("@asal", asal(comboBoxAsal.Text).ToString());
        cmd.Parameters.AddWithValue("@kondisi", checkedStatus());
        try
        {
            comm.Open();
            cmd.ExecuteNonQuery();
        }
        catch(MySqlException ex)
        (
            Console.WriteLine(ex.ToString());
        )
    }
}

you could also use

comboBoxGenre.Text instead of comboBoxGenre.SelectedValue
Sign up to request clarification or add additional context in comments.

6 Comments

i just curious why i need to refractor the code? i have another class consist of my mysqlconnectionstringbuilder.
i'm interested to know about the downvote :D, is it because of AddWithValue?
AddWithValue performs implicit casting of the datatypes. If I were you, use Add instead, see here Add()
okay, thanks your reply @JW. If i want to make the ComboBox list back to the clear field, i just need to place ComboBox.Text = "" or maybe there is proper way? thanks
if you are using generic Combobox, and the value of DropDownStyle is set to DropDownList, you need to add empty value first so you can set the value with ComboBox.Text = "" but if not then you can just free set it with ComboBox.Text = ""
|
1

There can be two reasons:

1.comboBoxGenre.SelectedValue will be null

2.kategori() will be returning null

you can handle null error by using Convert.ToString() instead of variable.toString()

so use this instead also for other lines

cmd.Parameters.AddWithValue("@genre", Convert.ToString(kategori(Convert.ToString(comboBoxGenre.SelectedValue))));

2 Comments

I would say that it would be a better practice to not swallow the null case with Convert.ToString(), but instead handle it by either allowing for a null category, or checking for it and taking appropriate action, like maybe throwing an exception.
thanks for your reply. i just edited the code and change the combobox method for getting the text.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.