Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8956585
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:49:14+00:00 2026-06-15T14:49:14+00:00

I’m completely new to the SQL command parameters (I have been briefly explained this

  • 0

I’m completely new to the SQL command parameters (I have been briefly explained this concept yesterday, and might not get it) and generally to the OOP but I enjoy it 😀

Here is the link to my question from yesterday 😉 Guys have really helped, but I now struggle to implement this in my app

I have actually built as an example:

  • Form “formConnection.cs” containing 2 user inputs “comboBoxEmployeeId” (selectedvalue is a int) + “txtDuration” (value is a decimal), on a click on “btnInsert” I process this:
    `

    sqlDbOperations sqlDbOp = new sqlDbOperations();`
    
    public void btnInsert_Click(object sender, EventArgs e)
    {
        //Create new contract
        var contract = new contract{
            employeeId = Convert.ToInt32(this.comboBoxEmployeeName.SelectedValue),
            duration = Convert.ToDecimal(this.txtDuration.Text)
        };
    
        //Insert command to Db
        sqlDbOp.insertContract();
    
        MessageBox.Show("Done");
    }
    
  • Class “contract.cs”

    class contract
    {
    public int employeeId { get; set; }
    public decimal duration { get; set; }
    }

  • Class “sqlDbOperations.cs”

    public class sqlDbOperations
    {
    //Connection string
    //SqlConnection myConnection = new SqlConnection(“ATLELAG786576\SQLEXPRESS;Initial Catalog=TEST;Integrated Security=False;Connect Timeout=30;User Instance=False;User ID=basicuser;Password=basicpw”);
    SqlConnection myConnection = new SqlConnection(ConfigurationManager.ConnectionStrings[“EngAdminSqlDbConnectionString”].ConnectionString);

        //Connection open
        //SqlConnection.Open() is a void function and does not return an error but throws an exception so remember to put it in a try/catch brace
        //Rather than having the program explode in front of the user
        public void openConnection()
        {
            //Connection open
            try
            {
                myConnection.Open();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
                MessageBox.Show(e.ToString());
            }
        }
    
        //Connection close
        //Try/catch because like SqlConnection.Open() it does not return errors but throws an exception instead
        public void closeConnection()
        {
            try
            {
                myConnection.Close();
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    
        contract contract = new contract();
    
        public void insertContract()
        {
            //Open
            openConnection();
    
            //Command
            SqlCommand myCommand = new SqlCommand("INSERT INTO tblContracts (EmployeeId, Duration) VALUES (@employeeId, @contractDuration)", myConnection);
    
            //Get values from form
            formConnection formInput = new formConnection();
    
            //Add parameters
            myCommand.Parameters.AddWithValue("@employeeId", contract.employeeId);
            myCommand.Parameters.AddWithValue("@contractDuration", contract.duration);
    
    
            //Execute
            myCommand.ExecuteNonQuery();
    
            //Close
            closeConnection();
    
    
    
    
        }
    }
    

This works without working
– When I put a “decimal” value like 2.7 in my txtDuration textbox I still got a message:

System.FormatException: Le format de la chaîne d’entrée est incorrect. = “The input string format is not correct”
à System.Number.StringToNumber(String str, NumberStyles options, NumberBuffer& number, NumberFormatInfo info, Boolean parseDecimal)
à System.Number.ParseDecimal(String value, NumberStyles options, NumberFormatInfo numfmt)
à System.Convert.ToDecimal(String value)
à SQLStatementParameters.formConnection.btnInsert_Click(Object sender, EventArgs e) dans D:\C#\Projects\SQLStatementParameters\SQLStatementParameters\formConnection.cs:ligne 26
à System.Windows.Forms.Control.OnClick(EventArgs e)”…..

  • Nothing is stored in the DB (as if the values are not transported from the form to the object ‘contract’ then to the method that does the INSERT), I have a new record but it’s all empty

What am I doing wrong?
Thank you for your help!

Brice

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T14:49:15+00:00Added an answer on June 15, 2026 at 2:49 pm

    The values are not transported into your sql command actually. The instance of class contract which you create in the class ‘sqlDbOperations’ is different from the one you create in btnInsert_Click. And you need to transport the one created in btnInsert_Click to sqlDbOp.insertContract(); to insert into DB.

    So you can do something like this:

    public void insertContract(contract con)
    {
        //Open
        openConnection();
    
        //Command
        SqlCommand myCommand = new SqlCommand("INSERT INTO tblContracts (EmployeeId,   Duration) VALUES (@employeeId, @contractDuration)", myConnection);
    
        //Get values from form
        formConnection formInput = new formConnection();
    
        //Add parameters
        myCommand.Parameters.AddWithValue("@employeeId", con.employeeId);
        myCommand.Parameters.AddWithValue("@contractDuration", con.duration);
    
    
        //Execute
        myCommand.ExecuteNonQuery();
    
        //Close
        closeConnection();
    }
    

    And for the exception ‘The input string format is not correct’. Maybe you can refer to this link

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
This could be a duplicate question, but I have no idea what search terms
I have been unable to fix a problem with Java Unicode and encoding. The
I'm not entirely sure how I managed to jack this up. http://pretty-senshi.com If you
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.