C# code with namespaces to insert data into SQL Server table with textbox input

Here’s an example C# code with namespaces to insert data into SQL Server table with textbox input:

using System;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace InsertDataToSQLTable
{
    public partial class Form1 : Form
    {
        // Define connection string for SQL Server
        private string connectionString = "Data Source=SERVERNAME;Initial Catalog=DATABASENAME;Integrated Security=True";

        public Form1()
        {
            InitializeComponent();
        }

        private void btnInsert_Click(object sender, EventArgs e)
        {
            // Get textbox input values
            string name = txtName.Text;
            string address = txtAddress.Text;
            int age = int.Parse(txtAge.Text);

            // Define SQL query to insert data into table
            string query = "INSERT INTO Persons (Name, Address, Age) VALUES (@Name, @Address, @Age)";

            try
            {
                // Open SQL Server connection
                using (SqlConnection connection = new SqlConnection(connectionString))
                {
                    connection.Open();

                    // Define SQL command with parameters
                    using (SqlCommand command = new SqlCommand(query, connection))
                    {
                        command.Parameters.AddWithValue("@Name", name);
                        command.Parameters.AddWithValue("@Address", address);
                        command.Parameters.AddWithValue("@Age", age);

                        // Execute SQL command
                        command.ExecuteNonQuery();

                        // Show success message
                        MessageBox.Show("Data inserted successfully.");
                    }
                }
            }
            catch (Exception ex)
            {
                // Show error message
                MessageBox.Show("Error: " + ex.Message);
            }
        }
    }
}

In this example, we first define the necessary namespaces: System for general functionality, System.Data.SqlClient for SQL Server specific functionality, and System.Windows.Forms for the form and textbox functionality.

Then, we create a form called Form1 and define a connection string to the SQL Server database we want to insert data into.

Next, we create a button called btnInsert and define its click event handler, which retrieves the values from three textboxes on the form (txtName, txtAddress, and txtAge), builds an SQL query string to insert the data into a table called Persons, and executes the SQL command with the parameterized values. If an error occurs during the execution, the error message will be displayed to the user. If the command executes successfully, a success message will be displayed to the user.

Note that this code assumes that you have a SQL Server database called DATABASENAME with a table called Persons that has three columns called Name, Address, and Age. You may need to modify the connection string and SQL query string to match your database and table names.

Leave a Comment