From the error message and code its clear that the
ConnectionString
value is not provided to the
SqlConnection
object.
refer
this[
^] for the connection string which suits for your application and try running it.
Correction to your code..
Always use
Parameterised query to avoid
Sql Injection[
^] attacks.
Since the test object is used no where, we can remove it.
private void button1_Click(object sender, EventArgs e)
{
var query = "insert into test.Users (Username,Password) values (@username,@password)";
SqlConnection con = new SqlConnection();
con.ConnectionString = "Your Connection string";
SqlCommand cmd = new SqlCommand(query, con);
cmd.Parameters.Add("@username", textBox1.Text.Trim());
cmd.Parameters.Add("@password", textBox2.Text.Trim());
try
{
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
catch (Exception es)
{
MessageBox.Show(es.Message);
}
}