none
sending comboBox values to Local SQL

    Question

  • Hey,

    This is the first time that I have created an app using a database therefore excuse the entire unawareness of how things like this work and how to do this and also my aplogies if I have placed this question in the wrong forums or anything like that, anyway.

    I have a C# application that has two comboBox's where I want to send their values to a database, the kind of value they will hold is like this: 1.01, 1.02 and so forth. I am lost as to how to make the database, what a datasource is, how to do this in genreal. I know how to make the databse (using VS 2010) and create a datasource from a table. I don't know what a key is or anything like that.

    So in basic I have a C# app where I want to send a comboBox's value (e.g 1.01) to a Local SQL database, I am lost as to how to create the database correctly and acutally send the value to the table. If anyone could help me specifcally with this, or offer me guidance with a link on how to go through this whole process that would be brilliant.

    Thank you,

     

     

    Saturday, October 16, 2010 3:26 PM

Answers

  • Below is a C# code snippet that shows one method to execute parameterized SQL insert statement.  You will need to tweak the connection string if your local instance is not the default one.  The database and table for this example can be created by running this this SQL script against your local instance. 

    Note that there are many other techniques to accomplish this (e.g. LINQ to SQL).  Regardless of the method you choose, the end result should be a parameterized SQL statement.

    CREATE DATABASE MyDatabase;
    GO
    USE MyDatabase;
    GO
    CREATE TABLE dbo.FooBar(
    	Decimal1 decimal(10,2) NOT NULL
    	,Decimal2 decimal(10,2) NOT NULL
    	);
    GO
    

     

        private void Form1_Load(object sender, EventArgs e)
        {
          //load combo box values
          this.comboBox1.Items.Add(new Decimal(1.01));
          this.comboBox1.Items.Add(new Decimal(1.02));
          this.comboBox2.Items.Add(new Decimal(2.01));
          this.comboBox2.Items.Add(new Decimal(2.02));
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
          //insert selected combobox values into the database
          var conn = new SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=SSPI");
          conn.Open();
          var cmd = new SqlCommand("INSERT INTO dbo.FooBar(Decimal1, Decimal2) VALUES(@Decimal1, @Decimal2);", conn);
          cmd.Parameters.AddWithValue("@Decimal1", this.comboBox1.SelectedItem);
          cmd.Parameters.AddWithValue("@Decimal2", this.comboBox2.SelectedItem);
          cmd.ExecuteNonQuery();
          conn.Close();
        }
    
    

    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/
    Saturday, October 16, 2010 4:17 PM
    Moderator

All replies

  • Below is a C# code snippet that shows one method to execute parameterized SQL insert statement.  You will need to tweak the connection string if your local instance is not the default one.  The database and table for this example can be created by running this this SQL script against your local instance. 

    Note that there are many other techniques to accomplish this (e.g. LINQ to SQL).  Regardless of the method you choose, the end result should be a parameterized SQL statement.

    CREATE DATABASE MyDatabase;
    GO
    USE MyDatabase;
    GO
    CREATE TABLE dbo.FooBar(
    	Decimal1 decimal(10,2) NOT NULL
    	,Decimal2 decimal(10,2) NOT NULL
    	);
    GO
    

     

        private void Form1_Load(object sender, EventArgs e)
        {
          //load combo box values
          this.comboBox1.Items.Add(new Decimal(1.01));
          this.comboBox1.Items.Add(new Decimal(1.02));
          this.comboBox2.Items.Add(new Decimal(2.01));
          this.comboBox2.Items.Add(new Decimal(2.02));
        }
        
        private void button1_Click(object sender, EventArgs e)
        {
          //insert selected combobox values into the database
          var conn = new SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=SSPI");
          conn.Open();
          var cmd = new SqlCommand("INSERT INTO dbo.FooBar(Decimal1, Decimal2) VALUES(@Decimal1, @Decimal2);", conn);
          cmd.Parameters.AddWithValue("@Decimal1", this.comboBox1.SelectedItem);
          cmd.Parameters.AddWithValue("@Decimal2", this.comboBox2.SelectedItem);
          cmd.ExecuteNonQuery();
          conn.Close();
        }
    
    

    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/
    Saturday, October 16, 2010 4:17 PM
    Moderator
  • You code is valid, what errors do you get?

    var cmd = new SqlCommand("CREATE DATABASE test", conn);

    cmd.ExecuteNonQuery();


    Best Regards, Uri Dimant SQL Server MVP http://dimantdatabasesolutions.blogspot.com/ http://sqlblog.com/blogs/uri_dimant/
    Monday, October 18, 2010 8:29 AM
    Moderator
  • Below is a C# code snippet that shows one method to execute parameterized SQL insert statement.  You will need to tweak the connection string if your local instance is not the default one.  The database and table for this example can be created by running this this SQL script against your local instance. 

    Note that there are many other techniques to accomplish this (e.g. LINQ to SQL).  Regardless of the method you choose, the end result should be a parameterized SQL statement.

    CREATE DATABASE MyDatabase;
    
    GO
    
    USE MyDatabase;
    
    GO
    
    CREATE TABLE dbo.FooBar(
    
    	Decimal1 decimal(10,2) NOT NULL
    
    	,Decimal2 decimal(10,2) NOT NULL
    
    	);
    
    GO
    
    

     

      private void Form1_Load(object sender, EventArgs e)
    
      {
    
       //load combo box values
    
       this.comboBox1.Items.Add(new Decimal(1.01));
    
       this.comboBox1.Items.Add(new Decimal(1.02));
    
       this.comboBox2.Items.Add(new Decimal(2.01));
    
       this.comboBox2.Items.Add(new Decimal(2.02));
    
      }
    
      
    
      private void button1_Click(object sender, EventArgs e)
    
      {
    
       //insert selected combobox values into the database
    
       var conn = new SqlConnection("Data Source=(local);Initial Catalog=MyDatabase;Integrated Security=SSPI");
    
       conn.Open();
    
       var cmd = new SqlCommand("INSERT INTO dbo.FooBar(Decimal1, Decimal2) VALUES(@Decimal1, @Decimal2);", conn);
    
       cmd.Parameters.AddWithValue("@Decimal1", this.comboBox1.SelectedItem);
    
       cmd.Parameters.AddWithValue("@Decimal2", this.comboBox2.SelectedItem);
    
       cmd.ExecuteNonQuery();
    
       conn.Close();
    
      }
    
    
    
    

    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/


    Thank you kindly for your reply however I am entirely lost. I try to make the databse using the:

    CREATE DATABASE MyDatabase;
    GO
    USE MyDatabase;
    GO
    CREATE TABLE dbo.FooBar(
    	Decimal1 decimal(10,2) NOT NULL
    	,Decimal2 decimal(10,2) NOT NULL
    	);
    GO
    
    code but got a bunch of errors, I don't know where I should be placing this code nor what you mean by "The database and table for this example can be created by running this this SQL script against your local instance. "....I do not know how to create the database at all and tried creating it via the Visual Studio 2010 UI, however when creating a table I couldn't use the variable decimal(10,2) . Please help, sorry for the stupidity on my behalf.
    Monday, October 18, 2010 8:47 AM
  • One way to run this ad-hoc script is with the Visual Studio T-SQL Editor.  Select Data-->T-SQL Editor--New Query Connection.  On the new connection dialog, specify your SQL instance for the server name (e.g. "(local)") and specify "master" for the database name.  Cliick OK twice and that will open a new query window.  Paste the script and press F5 to execute.

    If you have the SQL Serve client tools installed, you can run also run the script from a new query window in SQL Server Management Studio.


    Dan Guzman, SQL Server MVP, http://weblogs.sqlteam.com/dang/
    Monday, October 18, 2010 12:25 PM
    Moderator