#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Apr 2012
    Posts
    9
    Rep Power
    0

    Update sql from textbox error in c#


    I keep getting the error message "There was a problem updating your info, please try again" and nothing is updating, The connectionstring is fine because i'm using the same one for other sql's earlier in my program

    Please help, thank you !



    try
    {
    if (nameofuser.Text == "" || surnameofuser.Text == "" || cellnrofuser.Text == "")
    {
    MessageBox.Show("Please enter information in the edit boxes !", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }
    else
    {
    SqlConnection updateconn = new SqlConnection(myConnectionString);

    string UN = "dewald";
    updateconn.Open();
    SqlCommand updateuserinfo = new SqlCommand("UPDATE userinfo SET Name = @nameofuser , Surname = @surnameofuser Where username ='" + UN + "'", updateconn);
    updateuserinfo.Parameters.AddWithValue("@nameofuser", nameofuser.Text);
    updateuserinfo.Parameters.AddWithValue("@surnameofuser", surnameofuser.Text);
    updateuserinfo.Parameters.AddWithValue("@cellnrofuser", cellnrofuser.Text);
    updateuserinfo.ExecuteNonQuery();
    MessageBox.Show("Info was updated ", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
    updateconn.Close();

    }
    }
    catch
    {
    MessageBox.Show("There was a problem updating your info, please try again. ", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

    }
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Mar 2013
    Posts
    50
    Rep Power
    35
    Your catch is empty so you won't be able to see/handle an error when one occurs, I would add an Exception parameter to the catch statement and trace it out, either by stepping through the code in Visual Studio or by logging it to the console/text file.
    Code:
    try
    {
    	if (nameofuser.Text == "" || surnameofuser.Text == "" || cellnrofuser.Text == "")
    	{
    		MessageBox.Show("Please enter information in the edit boxes !", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
    	}
    	else
    	{ 
    		SqlConnection updateconn = new SqlConnection(myConnectionString);
    
    		string UN = "dewald";
    		updateconn.Open(); 
    		SqlCommand updateuserinfo = new SqlCommand("UPDATE userinfo SET Name = @nameofuser , Surname = @surnameofuser Where username ='" + UN + "'", updateconn);
    		updateuserinfo.Parameters.AddWithValue("@nameofuser", nameofuser.Text);
    		updateuserinfo.Parameters.AddWithValue("@surnameofuser", surnameofuser.Text);
    		updateuserinfo.Parameters.AddWithValue("@cellnrofuser", cellnrofuser.Text);
    		updateuserinfo.ExecuteNonQuery();
    		MessageBox.Show("Info was updated ", " ", MessageBoxButtons.OK, MessageBoxIcon.Information);
    		updateconn.Close();
    	}
    }
    catch (Exception ex)
    {
    	Console.WriteLine(ex.ToString());
    	
    	MessageBox.Show("There was a problem updating your info, please try again. ", " ", MessageBoxButtons.OK, MessageBoxIcon.Error);
    }

IMN logo majestic logo threadwatch logo seochat tools logo