Home > OS >  Simple select C# command - Beginner
Simple select C# command - Beginner

Time:10-18

Could you let me know where I am going wrong? I think its to do with the variable I have declared but I'm not sure

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.Data.SqlClient;

namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=xxx.xxx.xxx.xxx;Initial 
Catalog=VVM;Persist Security Info=True;User ID=sa;Password=xxxxx");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select dExpiryDate from tblStock where 
Stock_strBarcode= @Barcode", con);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);

        cmd.ExecuteNonQuery();

        con.Close();
        MessageBox.Show("Expiry Dates Updated! ;) ");
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
}
}

The exception I get is

    "System.Data.SqlClient.SqlException
    HResult=0x80131904
    Message=Must declare the scalar variable "@Barcode".
    Source=.Net SqlClient Data Provider
    StackTrace:
    at System.Data.SqlClient.SqlConnection.OnError(SqlException exception, Boolean 
    breakConnection, Action`1 wrapCloseInAction)
   at System.Data.SqlClient.SqlInternalConnection.OnError(SqlException exception, Boolean 
    breakConnection, Action`1 wrapCloseInAction)
    at System.Data.SqlClient.TdsParser.ThrowExceptionAndWarning(TdsParserStateObject stateObj, 
    Boolean callerHasConnectionLock, Boolean asyncClose)
    at System.Data.SqlClient.TdsParser.TryRun(RunBehavior runBehavior, SqlCommand cmdHandler, 
    SqlDataReader dataStream, BulkCopySimpleResultSet bulkCopyHandler, TdsParserStateObject 
    stateObj, Boolean& dataReady)
   at System.Data.SqlClient.SqlDataReader.TryConsumeMetaData()
   at System.Data.SqlClient.SqlDataReader.get_MetaData()
   at System.Data.SqlClient.SqlCommand.FinishExecuteReader(SqlDataReader ds, RunBehavior 

CodePudding user response:

You must add the parameter to the command before using it
This line should be moved up

cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);

Try this:

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.Data.SqlClient;

namespace WindowsFormsApp4
{
public partial class Form1 : Form
{
    public Form1()
    {
        InitializeComponent();
    }

    private void textBox1_TextChanged(object sender, EventArgs e)
    {

    }

    private void button1_Click(object sender, EventArgs e)
    {
        SqlConnection con = new SqlConnection("Data Source=xxx.xxx.xxx.xxx;Initial 
Catalog=VVM;Persist Security Info=True;User ID=sa;Password=xxxxx");
        con.Open();
        SqlCommand cmd = new SqlCommand("Select dExpiryDate from tblStock where 
Stock_strBarcode= @Barcode", con);
cmd.Parameters.AddWithValue("@Barcode", textBox1.Text);
        SqlDataAdapter da = new SqlDataAdapter(cmd);
        DataTable dt = new DataTable();
        da.Fill(dt);
        dataGridView1.DataSource = dt;
        

        cmd.ExecuteNonQuery();

        con.Close();
        MessageBox.Show("Expiry Dates Updated! ;) ");
    }

    private void label2_Click(object sender, EventArgs e)
    {

    }

    private void label3_Click(object sender, EventArgs e)
    {

    }

    private void label4_Click(object sender, EventArgs e)
    {

    }
}
}
  • Related