K
K
keknyx2022-04-14 16:13:57
C++ / C#
keknyx, 2022-04-14 16:13:57

How to execute sql query with button?

sql query and it was executed on button click:

spoiler
private void button3_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
                $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES ((SELECT TOP 1 Name_Of_Product FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Price FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 CapacityCBM FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Provider FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderAddress FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderPhone FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderEmail FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Date_Of_Delivery FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Quantity FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Number_Of_Contract FROM Postavki ORDER BY NEWID())", sqlConnection);
           command.ExecuteNonQuery();
        }


mistake:
System.Data.SqlClient.SqlException: "Incorrect syntax near ')'.

what is the problem?

All form code:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Configuration;
using System.Data.SqlClient;

namespace SQL_DB
{
    public partial class Form1 : Form
    {

        private SqlConnection sqlConnection = null;

        private SqlDataAdapter adapter = null;

        private DataTable table = null;

        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            sqlConnection = new SqlConnection(ConfigurationManager.ConnectionStrings["KeyDB"].ConnectionString);

            sqlConnection.Open();

            adapter = new SqlDataAdapter("SELECT * FROM Postavki", sqlConnection);

            table = new DataTable();

            adapter.Fill(table);

            dataGridView1.DataSource = table;

            if (sqlConnection.State == ConnectionState.Open)
            {
                MessageBox.Show("Подключение установлено");
            }
        }

        private void button1_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
                $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract) VALUES (@Name_Of_Product, @Price, @CapacityCBM, @Provider, @ProviderAddress, @ProviderPhone, @ProviderEmail, @Date_Of_Delivery, @Quantity, @Number_Of_Contract)",
                sqlConnection);

            DateTime date = DateTime.Parse(textBox8.Text);

            command.Parameters.AddWithValue("Name_Of_Product", textBox1.Text);
            command.Parameters.AddWithValue("Price", textBox2.Text);
            command.Parameters.AddWithValue("CapacityCBM", textBox3.Text);
            command.Parameters.AddWithValue("Provider", textBox4.Text);
            command.Parameters.AddWithValue("ProviderAddress", textBox5.Text);
            command.Parameters.AddWithValue("ProviderPhone", textBox6.Text);
            command.Parameters.AddWithValue("ProviderEmail", textBox7.Text);
            command.Parameters.AddWithValue("Date_Of_Delivery", $"{date.Month}/{date.Day}/{date.Year}");
            command.Parameters.AddWithValue("Quantity", textBox9.Text);
            command.Parameters.AddWithValue("Number_Of_Contract", textBox10.Text);

            MessageBox.Show(command.ExecuteNonQuery().ToString());
        }

        private void button2_Click(object sender, EventArgs e)
        {
            table.Clear();

            adapter.Fill(table);

            dataGridView1.DataSource = table;



        }

        private void dataGridView1_CellContentClick(object sender, DataGridViewCellEventArgs e)
        {

        }

       

        

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void button3_Click(object sender, EventArgs e)
        {
            SqlCommand command = new SqlCommand(
               $"INSERT INTO [Postavki] (Name_Of_Product, Price, CapacityCBM, Provider, ProviderAddress, ProviderPhone, ProviderEmail, Date_Of_Delivery, Quantity, Number_Of_Contract)" + "VALUES ((SELECT TOP 1 Name_Of_Product FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Price FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 CapacityCBM FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Provider FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderAddress FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderPhone FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 ProviderEmail FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Date_Of_Delivery FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Quantity FROM Postavki ORDER BY NEWID()), (SELECT TOP 1 Number_Of_Contract FROM Postavki ORDER BY NEWID())", sqlConnection);
            command.ExecuteNonQuery();
        }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
H
hint000, 2022-04-14
@keknyx

System.Data.SqlClient.SqlException: "Incorrect syntax near ')'."

In my opinion, he hints that you are missing one parenthesis at the end of the query:
...ORDER BY NEWID())", sqlConnection);
Isn't that how it should be?:
...ORDER BY NEWID()))" ,sqlConnection);

A
Anton Antonov, 2022-04-18
@Shellon

He just hints at the parenthesis. And why are you sorting by NEWID() and not just NEWID?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question