title: MySql exception : Connection need to valid and open. C#
body: <p>I am trying to insert and retrieve data from MySql database using RESTful api. When I performing an insert query, I am getting exception message : Connection need to valid and open.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Web;
  using MySql.Data;
  
  
  using WorkAppApi.Models;
  
  namespace WorkAppApi
  {
  public class DBConn
  {
      private MySql.Data.MySqlClient.MySqlConnection conn;
  
      public DBConn()
      {
          string myconnstr;
          myconnstr = "server=192.168.0.103;uid=root;pwd=ganga10lfc;database=readingmeterdb";
          try
          {
              conn = new MySql.Data.MySqlClient.MySqlConnection();
              conn.ConnectionString = myconnstr;
              conn.Open();
          }
          catch (MySql.Data.MySqlClient.MySqlException ex)
          {
  
          }
  
  
      }
  
      public long addMachine(machines machinetosave)
      {
  
  
          String sqlstr = "INSERT INTO machines (machineDesc) VALUES ('" + machinetosave.MachineDesc + "')";
          MySql.Data.MySqlClient.MySqlCommand cmd = new MySql.Data.MySqlClient.MySqlCommand(sqlstr,conn);
  
          cmd.ExecuteNonQuery();
  
          long id = cmd.LastInsertedId;
          return id;
      }
  
  
  }
  }
  </code></pre>
  
  <blockquote>
    <p>"Message": "An error has occurred.",
      "ExceptionMessage": "Connection must be valid and open.",
      "ExceptionType": "System.InvalidOperationException",
      "StackTrace": "   at MySql.Data.MySqlClient.MySqlCommand.CheckState()\r\n   at MySql.Data.MySqlClient.MySqlCommand.ExecuteReader(CommandBehavior behavior)\r\n   at MySql.Data.MySqlClient.MySqlCommand.ExecuteNonQuery()\r\n   at WorkAppApi.DBConn.addMachine(machines machinetosave) in C:\Users\User\Desktop\WorkAppApi\WorkAppApi\DBConn.cs:line 41\r\n   at WorkAppApi.Controllers.MachinesController.Post(machines value) in C:\Users\User\Desktop\WorkAppApi\WorkAppApi\Controllers\MachinesController.cs:line 31\r\n   at lambda_method(Closure , Object , Object[] )\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.&lt;>c__DisplayClassc.b__6(Object instance, Object[] methodParameters)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ActionExecutor.Execute(Object instance, Object[] arguments)\r\n   at System.Web.Http.Controllers.ReflectedHttpActionDescriptor.ExecuteAsync(HttpControllerContext controllerContext, IDictionary`2 arguments, CancellationToken cancellationToken)\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ApiControllerActionInvoker.d__0.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Controllers.ActionFilterResult.d__2.MoveNext()\r\n--- End of stack trace from previous location where exception was thrown ---\r\n   at System.Runtime.CompilerServices.TaskAwaiter.ThrowForNonSuccess(Task task)\r\n   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)\r\n   at System.Web.Http.Dispatcher.HttpControllerDispatcher.d__1.MoveNext()"</p>
  </blockquote>