using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
namespace Trading
{
class DBConnection
{
private SqlConnection _con;
public SqlCommand cm;
public DBConnection()
{
_con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Public\TTMS\dbTSIS.mdf;Integrated Security=True;Connect Timeout=30");
}
public void SqlQuery(string queryText)
{
cm = new SqlCommand(queryText, _con);
if (_con.State == ConnectionState.Closed)
{
_con.Open();
}
cm.ExecuteNonQuery();
_con.Close();
}
}
}
public partial class FrmUserEntry : Form
{
private DBConnection con;
// SqlConnection con = new SqlConnection(@"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Users\Public\TTMS\dbTSIS.mdf;Integrated Security=True;Connect Timeout=30");
//SqlCommand cm = new SqlCommand();
public FrmUserEntry()
{
InitializeComponent();
rsBtnUpdate.Enabled = false;
}
private void rsBtnSave_Click(object sender, EventArgs e)
{
try
{
con = new DBConnection();
if (MessageBox.Show("Are you sure you want to save this user?", "Saving Record",
MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
con.SqlQuery("INSERT INTO tbUser(fullname,username,password,contactno)VALUES(@fullname,@username,@password,@contactno)");
con.cm.Parameters.AddWithValue("@fullname", mTxtFullname.Text);
con.cm.Parameters.AddWithValue("@username", mTxtUserName.Text);
con.cm.Parameters.AddWithValue("@password", mTxtPassword.Text);
con.cm.Parameters.AddWithValue("@contactno", mTxtContact.Text);
MessageBox.Show("User has been Successfully saved.");
Clear();
}
catch(Exception ex)
{
MessageBox.Show(ex.Message);
}
}
when i save the data error like You must declare the scaler variable @fullname.
pls help
Naimish MakwanaPosted May 15, 2023, 9:10 AM
The error message "You must declare the scalar variable '@fullname'" is indicating that the parameter '@fullname' is not being recognized as a valid parameter in the SQL query. This error commonly occurs when the parameter is not added correctly to the SqlCommand object.
To fix the issue, you need to make sure that you add the parameters to the SqlCommand object before executing the query. In your code, you are adding the parameters after executing the query, which is causing the error.
Here's the updated code that adds the parameters before executing the query:
Thanks
Mohamed Azarudeen ZPosted May 14, 2023, 5:11 PM
It looks like you are trying to execute a parameterized SQL query but you have not declared the parameters correctly. The error message "You must declare the scalar variable @fullname" indicates that the SQL query is expecting a parameter called "@fullname" but it has not been declared.
To fix this error, you need to declare the parameters before executing the SQL query. You can do this by calling the `Parameters.AddWithValue()` method on the `SqlCommand` object, like this:
con.cm.Parameters.AddWithValue("@fullname", mTxtFullname.Text);
con.cm.Parameters.AddWithValue("@username", mTxtUserName.Text);
con.cm.Parameters.AddWithValue("@password", mTxtPassword.Text);
con.cm.Parameters.AddWithValue("@contactno", mTxtContact.Text);
Make sure you declare all the parameters that your SQL query expects.
Also, you should move the parameter declaration before the `ExecuteNonQuery()` method is called, like this:
con.cm.Parameters.AddWithValue("@fullname", mTxtFullname.Text);
con.cm.Parameters.AddWithValue("@username", mTxtUserName.Text);
con.cm.Parameters.AddWithValue("@password", mTxtPassword.Text);
con.cm.Parameters.AddWithValue("@contactno", mTxtContact.Text);
con.cm.ExecuteNonQuery();
This will ensure that the parameters are declared before the SQL query is executed.