0

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");
        }
    }

}

}

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
Adam
  • 45
  • 1
  • 7
  • Chances are you're using the wrong connection string. "Microsft visual Express C# 2010" is not a product, I assume you mean through Visual Studio Express. How did you create the database? – Terry Apr 19 '12 at 17:14
  • 1
    You say that you get no feedback, so your program just runs or...? – Joshua Drake Apr 19 '12 at 17:17
  • 1
    remove the try/catch blocks as these are swallowing the actual error. you can review the error type, message and stack trace to determine what the exact problem is. also the try catches will create more problem when the user clicks "ok" on the msg box. – Jason Meckley Apr 19 '12 at 17:19
  • i think its visual studio express but i am using the C# Version of it. i created the DB on the program itself it has options to create a database just as you would on SQL server management express. or MYSQL. – Adam Apr 19 '12 at 17:19

1 Answers1

3

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.

user7116
  • 63,008
  • 17
  • 141
  • 172
  • good catch. also, you'll want to properly dispose of the ado.net objects to prevent memory leaks. – Jason Meckley Apr 19 '12 at 17:20
  • There are quite a number of improvements which could be made, however, this being homework for the OP I've left out more than the problem at hand. – user7116 Apr 19 '12 at 17:21
  • now i am getting feedback. i got an error but now i have something to work off thanks Six. – Adam Apr 19 '12 at 17:26