0

Hi there im new to C# and SQL. But my problem is with SQL im trying to conect to a Database i created on visual studio 2010. it trhows me the exception of cannot convert vachar to float. Here is the code.

private void execute()
{
     ///Here we atempt to connect to the local DB
     System.Data.SqlClient.SqlConnection SqlConnection1 = new   
     System.Data.SqlClient.SqlConnection(@"Data 
     Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated 
     Security=True;User Instance=True");
     SqlConnection1.Open();
     System.Data.SqlClient.SqlTransaction tx = SqlConnection1.BeginTransaction();
     System.Data.SqlClient.SqlCommand cmd = SqlConnection1.CreateCommand();
     cmd.Transaction = tx;
     ///Here we try to operate on the Local DB
     try
     {                
          cmd.CommandText = "INSERT INTO EmployeeInfo(EmployeeSalary,EmployeePhone,EmployeeDeskPhone,EmployeeSSN,EmployeeEmergencyContactName,EmployeeEmergencyContactPhone,EmployeeFirstName,EmployeeLastName) VALUES  
            (@_employeeSalary,'@_employeePhone','@_employeeDeskPhone','@_employeeSSN','@_employeeEmergencyContactName','@_employeeEmergencyContactPhone','@_employeeFirstName','@_employeLastName')";
          cmd.Parameters.AddWithValue("@_employeeSalary", _employeeSalary);
          cmd.Parameters.AddWithValue("@_employeePhone", _employeePhone);
          cmd.Parameters.AddWithValue("@_employeeDeskPhone", _employeeDeskPhone);
          cmd.Parameters.AddWithValue("@_employeeSSN", _employeeSSN);
          cmd.Parameters.AddWithValue("@_employeeEmergencyContactName",  
            _employeeEmergencyContactName);
          cmd.Parameters.AddWithValue("@_employeeEmergencyContactPhone", 
            _employeeEmergencyContactPhone);
          cmd.Parameters.AddWithValue("@_employeeFirstName", _employeeFirstName);
          cmd.Parameters.AddWithValue("@_employeeLastName", _employeeLastName);
          cmd.ExecuteNonQuery();
          tx.Commit();
     }
     ///If unsuccesful we rollback changes 
     catch (System.Data.SqlClient.SqlException sqlException)
     {
          System.Windows.Forms.MessageBox.Show(sqlException.Message);
          tx.Rollback();
          Console.WriteLine("**** There was Trouble Houston, with the Insert****");
     }
     ///Thhis piee of code always executes
     finally
     {
          SqlConnection1.Close();
          Console.WriteLine("***** if theres no trouble before this,  then It succesfully added the record to the database*****");
     } 
}

the variable emplyeeSalary is set to float and is the only one so i don get whjy it trhows that message. Please any help would be deeply apreciated.

Batuhan Coşkun
  • 2,961
  • 2
  • 31
  • 48
  • 1
    Stop, stop, stop using User Instance and AttachDbFileName. Attach your database to a proper instance of SQL Server, and connect by database name, not via the MDF file. Your current approach is deprecated and also leads to problems debugging when you update something in your program and you can't see it in SSMS or another instance of your program - it's because they have *different copies of the database*. – Aaron Bertrand Sep 18 '14 at 15:11
  • Oh ok , thanks a lot as i said im really new to sql and i have been going on some old Q/A's from here:) . thank you for your input and could you please point me i n the proper direction via some links or some specific terms for me to google and learn about. I barely understood the terms you used there , and thats my bad – yuyocollores Sep 18 '14 at 15:17
  • 1
    http://stackoverflow.com/questions/11178720/whats-the-issue-with-attachdbfilename/11178889#11178889 – Aaron Bertrand Sep 18 '14 at 15:19
  • Thanks aaron i Deeply apreciate it – yuyocollores Sep 18 '14 at 15:25
  • Just for good measure was that the only deprecated piece of code you saw or is there more i could change? – yuyocollores Sep 18 '14 at 15:27
  • 1
    I like to set explicit data type / precision / scale for my parameters, rather than let the command object infer it based on the value of the parameter. – Aaron Bertrand Sep 18 '14 at 15:45

1 Answers1

3

Your problem is quite simple, you've placed the command parameters inside single quotes for some reason. Change them to the following:

cmd.CommandText = @"INSERT INTO 
  EmployeeInfo( EmployeeSalary ,EmployeePhone, EmployeeDeskPhone, EmployeeSSN,
  EmployeeEmergencyContactName, EmployeeEmergencyContactPhone, EmployeeFirstName,
  EmployeeLastName) VALUES  
  (@_employeeSalary,@_employeePhone,@_employeeDeskPhone,@_employeeSSN,
   @_employeeEmergencyContactName,@_employeeEmergencyContactPhone,
   @_employeeFirstName, @_employeLastName)";
Daniel Gimenez
  • 18,530
  • 3
  • 50
  • 70
  • Thanks a lot that seemed to do the trick no errors from the try/catch although the data wasnt permanently inserted on the database, does it only inserts it for the purpose of the runtime of the program or is here something else going on... – yuyocollores Sep 18 '14 at 15:13
  • If there are no errors, then it should've permanently inserted the data, you might want to re-query your table to be sure. – Daniel Gimenez Sep 18 '14 at 15:16