Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie
Hi there,
There is an issue with role permissions that is being worked on at the moment.
If you are having trouble with access or permissions on regional forums please post here to get access: https://www.boards.ie/discussion/2058365403/you-do-not-have-permission-for-that#latest

SQL Command Insert

  • 27-01-2009 3:06pm
    #1
    Registered Users, Registered Users 2 Posts: 325 ✭✭


    Hi

    I,m using Visual Studio 2008, its a Windows Application and I'm using C#

    I'm trying to insert a new user into a database - they have a username and password.

    This is the code that I have done - its running without errors but its not inserting:

    private void btnSubmit_Click(object sender, EventArgs e)
    {
    var vusername = txtUsername.Text.Trim();
    var vpassword = txtPassword.Text.Trim();
    var vconfirmpass = txtConfirmPass.Text.Trim();


    if (vpassword == "" )
    {
    MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info");
    txtUsername.Select();
    return;
    }
    if (vusername == "")
    {
    MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info");
    txtUsername.Select();
    return;
    }

    if (vconfirmpass == "")
    {
    MessageBox.Show("You are missing information. Please make sure that both the username and password fields are filled out.", "Missing Info");
    txtUsername.Select();
    return;
    }

    SqlConnection con1 = new SqlConnection();
    con1.ConnectionString = "Data Source=.\\SQLEXPRESS;AttachDbFilename=E:\\PROTOTYPE1\\PROTOTYPE\\Users.mdf;Integrated Security=True;Connect Timeout=30;Asynchronous Processing=true;User Instance=True";

    SqlCommand cmd = new SqlCommand("SELECT [Username] From Users WHERE [Username] = '"+ txtUsername.Text.ToString()+"'", con1);
    con1.Open();

    SqlDataReader dr = cmd.ExecuteReader();
    if (dr.HasRows)
    {
    MessageBox.Show("Username already exists, please choose another!!", "Information");
    txtUsername.Select();
    dr.Close();
    return;
    }
    else
    dr.Close();
    cmd1 = new SqlCommand("INSERT INTO Users ([Username],[Password]) VALUES('vusername','vpassword')", con1);
    cmd1.ExecuteNonQuery();
    MessageBox.Show("User Added!", "Information");
    con1.Close();
    }


    Cheers


Comments

  • Closed Accounts Posts: 5,284 ✭✭✭pwd


    Can you run a trace on the database (use sql profiler if you have access)? This will let you see the sql as it it is executed on the database, which should let you see where the problem is.


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    Check your DB for a user 'vusername' with a password of 'vpassword'.....

    Odds are thats your problem right there.

    cmd1 = new SqlCommand("INSERT INTO Users ([Username],[Password]) VALUES('" + vusername + "','" + vpassword + "')", con1);

    The above should solve it


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    Cheers for that - managed to get it working but I'm having a different problem with it now

    Its updating the database but not when the program is running - how can I refresh the database while the program is running?


    Cheers


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    doyler442 wrote: »
    Cheers for that - managed to get it working but I'm having a different problem with it now

    Its updating the database but not when the program is running - how can I refresh the database while the program is running?


    Cheers

    What exactly do you mean? Sorry but it does not make much sense to me as you have put it.


  • Registered Users, Registered Users 2 Posts: 23,212 ✭✭✭✭Tom Dunne


    Do you need to commit? Seems to make sense that when the program is running, data is not there, but when the program ends (and presumably commits), you can see the data.


  • Advertisement
  • Registered Users, Registered Users 2 Posts: 610 ✭✭✭nialo


    Is it cause your not refreshing your screen after you insert. if you have a grid on the screen it will need to be refreshed to see the new data. also not sure if you should have Asynchronous Processing=true; in your connection string. remove that mite be an idea.


  • Registered Users, Registered Users 2 Posts: 325 ✭✭doyler442


    nialo wrote: »
    Is it cause your not refreshing your screen after you insert. if you have a grid on the screen it will need to be refreshed to see the new data. also not sure if you should have Asynchronous Processing=true; in your connection string. remove that mite be an idea.

    Thanks for this, its not me doing the coding just trying to help my friend out.

    Could you let me know how to refresh the screen after the insert?


    Really appreciate your help with these problems


  • Registered Users, Registered Users 2 Posts: 2,494 ✭✭✭kayos


    If he followed a good coding pratice he should have a function(s) that would populate the screen or its parts. Once you have inserted the new reocrd correctly then just call those functions.


Advertisement