Reputation: 55
I'm creating a database so I can store usernames and passwords. I have successfully created a DB on Microsft Visual Express C# 2010.
My program seems to be working fine, and produces no error message, except that I'm not getting any feedback: it looks like my program is not actually making the request to the database, or not returning the results. What's going on?
private void button1_Click(object sender, EventArgs e)
{
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
string username = textBox1.Text;
string password = maskedTextBox1.Text;
string sqlquery = ("SELECT * FROM User WHERE Username = '" + textBox1.Text + "'");
sqlquery = "INSERT INFO [User] (Username, Password) VALUES ('" + textBox1.Text + "','" + maskedTextBox1.Text + "')";
SqlCommand Command = new SqlCommand(sqlquery, cn);
Command.Parameters.AddWithValue("Username", username);
Command.Parameters.AddWithValue("Password", password);
}
private void Form1_load(object sender, EventArgs e)
{
string connection = @"Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True";
SqlConnection cn = new SqlConnection(connection);
try
{
cn.Open();
}
catch (Exception)
{
MessageBox.Show("Did not connect");
}
}
}
}
Upvotes: 0
Views: 174
Reputation: 64098
You're not actually executing the SqlCommand
, so it seems logical that you would not "see" anything happen. Try adding the following line to the end of button1_Click
:
Command.ExecuteNonQuery(); // actually execute the INSERT INFO... query
You have some issues with your SQL code, but you'll receive exceptions for them once you execute the command.
Upvotes: 3