title: C# Must declare the scalar variable "@SomeThing" in insert query
body: <p>I am working on .net c# , I want to insert some data into a table.</p>
  
  <p>I am write query in Visual Studio and not using stored procedure(s).</p>
  
  <p>This is my query </p>
  
  <pre><code>SqlCommand cmd2 = new SqlCommand("insert into Device_Events (Device_ID, Event_ID, Occurrence_Time, Recovery_Time) values (@Device_ID , @Event_ID, @Occurrence_Time, @Recovery_Time)", con);
  </code></pre>
  
  <p>And this is my C# code for passing values in it </p>
  
  <pre><code>// For Events
  string formatString = "yyMMddHHmmss";
  DateTime Occurrence_Time, Recovery_Time;
  string strOccurrence = Meter_data.Substring(161, 12);
  string strRecovery = Meter_data.Substring(173, 12);
  
  cmd2.Parameters.AddWithValue("@Device_ID", device_Id);
  cmd2.Parameters.AddWithValue("@Event_ID", event_Id);
  
  if (DateTime.TryParseExact(strOccurrence, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out Occurrence_Time))
  {
      if (DateTime.TryParseExact(strRecovery, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out Recovery_Time))
      {
          cmd2.Parameters.AddWithValue("@Occurrence_Time", SqlDbType.DateTime).Value = Occurrence_Time;
          cmd2.Parameters.AddWithValue("@Recovery_Time", SqlDbType.DateTime).Value = Recovery_Time;
      }
  }
  
  int Device_Events_rows_executed = cmd2.ExecuteNonQuery();
  Console.WriteLine("Rows Executed: '{0}'", Device_Events_rows_executed);
  </code></pre>
  
  <p>It doesn't goes into the <code>if</code> part and i am getting an exception named <code>Must declare the scalar variable "@Occurrence_Time"</code></p>
  
  <p>Also i have read this <a href="https://stackoverflow.com/a/20330905/6856823">link</a> but unable to find any help</p>
  
  <p><strong>Updated Code</strong></p>
  
  <p>After a suggestion I have added following piece of code </p>
  
  <pre><code>            cmd2.Parameters.Add("@Device_ID", SqlDbType.VarChar, 50).Value = device_Id;
              cmd2.Parameters.Add("@Event_ID", SqlDbType.VarChar, 50).Value = event_Id;
              cmd2.Parameters.Add("@Occurrence_Time", SqlDbType.DateTime, 50).Value = DBNull.Value;
              cmd2.Parameters.Add("@Recovery_Time", SqlDbType.DateTime, 50).Value = DBNull.Value;
  
              string formatString = "yyMMddHHmmss";
              DateTime Occurrence_Time, Recovery_Time;
              string strOccurrence = Meter_data.Substring(161, 12);
              string strRecovery = Meter_data.Substring(173, 12);
  
              if (DateTime.TryParseExact(strOccurrence, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out Occurrence_Time))
              {
                  cmd2.Parameters["@Occurrence_Time"].Value = Occurrence_Time;
              }
              if (DateTime.TryParseExact(strRecovery, formatString, CultureInfo.InvariantCulture, DateTimeStyles.None, out Recovery_Time))
              {
                  cmd2.Parameters["@Recovery_Time"].Value = Recovery_Time;
              }
  </code></pre>
  
  <p>Although it's not showing me and error or exception but still it doesn't gets into the both <code>if</code> conditions</p>
  
  <p>Any help would be highly appreciated</p>