4
Answers

SQL insert and Update

Ekrem Tapan

Ekrem Tapan

8y
265
1
hi everyone, when i want to insert sql some value i have a getting “executenonquery connection property has not been initialized” error. but i dont know why ? 
 
everything is clearly code is below
 
anyone have a any idea for this trouble ?
 
  1. for (int i = 0; i < this._blocks.Count; i++)  
  2.            {  
  3.                if (this._blocks[i].Visible && !string.IsNullOrEmpty(this._blocks[i].Year))  
  4.                {  
  5.   
  6.                    string constr = ConfigurationManager.AppSettings["ConnInfo"];  
  7.                    using (SqlConnection con = new SqlConnection(constr))  
  8.                    {  
  9.                        string sql = string.Format(@"IF EXISTS (SELECT * FROM INDUS_OVER_STAT WHERE YEARS = {0}) UPDATE  INDUS_OVER_STAT   
  10.                                                    SET          
  11.                                                        VAL_1 = {1},   
  12.                                                        VAL_2 = {2},   
  13.                                                        VAL_3 = {3},  
  14.                                                        TARGET_SID = '{4}'  
  15.                                                    WHERE    YEARS = {0}  
  16.                                                    ELSE  
  17.                                                    INSERT    INDUS_OVER_STAT(TARGET_SID, YEARS,VAL_1,VAL_2,VAL_3)  
  18.                                                     VALUES    ('{4}', {0},{1},{2},{3})", this._blocks[i].Year, this._blocks[i].MS,this._blocks[i].BNU,this._blocks[i].Emp,this._groupsSId);  
  19.   
  20.                        
  21.                        using (SqlCommand cmd = new SqlCommand(sql))  
  22.                        {  
  23.                            con.Open();  
  24.                            cmd.ExecuteNonQuery();  
  25.                            con.Close();  
  26.                        }  
  27.                    }  
  28.   
  29.                }  
  30.            }  
 
Answers (4)