0

I have this error when I try to save some data to my sql (express) through the software I have create.

An unhandled exception of type 'System.InvalidOperationException' occurred in System.Data.dll. Additional information: ExecuteNonQuery: Connection property has not been initialized.

Here is a my code :

    private void save_medicine_Click(object sender, EventArgs e)
    {
        cn.Open();

        if (nameBox.Text != "" & companyBox.Text != "" & categoryBox.Text != "" & 
           amountBox.Text != "" & restockBox.Text != "" & buyBox.Text != "" & 
           sellBox.Text != "" & qualityComboBox.Text !=""  )
        {
            cmd.CommandText = "insert into drug_info (name,company_id,category,
                               expiration_date,stock_level,
                               critical_level_drug,cost_buy,cost_sell,drug_quality) 
                               values ('"+nameBox.Text+"','"+companyBox.Text+"','"+
                               categoryBox.Text+"','"+dateTimePicker1+"','"+amountBox+"','"
                                +restockBox+"','"+buyBox+"','"+sellBox+"','"
                                +qualityComboBox+"')";
            cmd.ExecuteNonQuery();
            cmd.Clone();

        }
    }
}
Brad
  • 359
  • 5
  • 21
dbexec
  • 274
  • 9
  • 19
  • 1
    Why did you post a screenshot of code? It's not readable at all. Please post the code instead. – Adam May 27 '12 at 11:12

3 Answers3

0

You have a class variable cn and a local variable cn in rMedecine_reg_load. That doesn't seem right.

The error simply means you are trying to use cmd before assigning a connection to it.

C.Evenhuis
  • 25,996
  • 2
  • 58
  • 72
  • Ok i found what i had to do.. now i have another error An unhandled exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll Additional information: Conversion failed when converting date and/or time from character string. – dbexec May 27 '12 at 11:26
  • Indeed, you should probably initialize cmd using either cmd = cn.CreateCommand() or cmd = new SqlCommand(cn) (I think the syntax is) – Peter Davidsen May 27 '12 at 11:27
0

try it like this

     if (nameBox.Text != "" & companyBox.Text != "" & categoryBox.Text != "" &  amountBox.Text != "" & restockBox.Text != "" & buyBox.Text != "" & sellBox.Text != "" & qualityComboBox.Text !=""  )
    {

        string sql = "insert into drug_info (name,company_id,category,expiration_date,stock_level,critical_level_drug,cost_buy,cost_sell,drug_quality) values ('"+nameBox.Text+"','"+companyBox.Text+"','"+categoryBox.Text+"','"+dateTimePicker1+"','"+amountBox+"','"+restockBox+"','"+buyBox+"','"+sellBox+"','"+qualityComboBox+"')";
        SqlCommand cmd = new SqlCommand(sql,cn);
        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
        MessageBox.Show("Τα στοιχεία καταχωρήθηκαν στη Βάση");
    }

that should probably work.. altho i dont know why values like "amountBox" and "buyBox" dont have a .Text attatched to them. I dont know where those values come from, but you might need to add .Text if they are textboxes. if they are, and you try to insert them into your database like that you will get an error, because you cant insert something that has a type of "Textbox" in a database.

Thousand
  • 6,562
  • 3
  • 38
  • 46
  • they are int at my db if i add .text it will brake. so i am trying to find what i must add at the end. – dbexec May 27 '12 at 11:39
  • try Convert.ToInt32(amountBox.Text), that should parse them to Integers. the dateTimepicker, is that a jquery timepicker? because you're going to have to parse that aswell then – Thousand May 27 '12 at 11:44
  • ok i have done the way you told me i added to all int@myDB convert.ToInt32 and my decimal@myDB convert.ToDouble **BUT** i got this error now.. _Additional information: Arithmetic overflow error converting varchar to data type numeric._ BUt all my nvarchar textbox are like this textbox.text ... – dbexec May 27 '12 at 11:55
0

First of all: use parameters. You can read about them here.

Another thing is this line:

cmd.Clone();

I bet you wanted to close the command. But there is no way to close a SqlCommand. Just remove the line.

You should also clean up the mess with 2 connections as mentioned in other answer.

Adam
  • 26,549
  • 8
  • 62
  • 79