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