1

This is the code for the WebService:

[WebMethod]

public MyUser GetProfile(string username)
{
    MyUser user = null;
    try
    {
        OleDbCommand cmd = conn.CreateCommand();

        cmd.CommandText = @"SELECT * FROM [Details] WHERE [UserName] = '" + Username + "'";

        //Check for matches
        OleDbDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow);

        if (dbReader.HasRows)
        {
            user = new MyUser()
            {
                FirstName = (string)dbReader["FirstName"],
                Country = (string)dbReader["Country"],
                DateOfBirth = (string)dbReader["DateOfBirth"],
                //DateOfBirth = DateTime.Parse(dbReader["DateOfBirth"].ToString()),
                EmailAddress = (string)dbReader["EmailAddress"],
                Password = (string)dbReader["Password"],
                Surname = (string)dbReader["Surname"],
                Username = (string)dbReader["UserName"]
            };
        }

        dbReader.Close();
        dbReader.Dispose();
    }
    catch
    { }

    return user;
}//end ViewProfile>

And here's my code for calling the WebService

 protected void Page_Load(object sender, EventArgs e)
{
    lblUsername.Text = User.Identity.Name + "'s Profile";
    Service s = new Service();

    MyUser user = s.GetProfile(User.Identity.Name);

    try
    {
        txtCountry.Text = user.Country;
        txtDOB.Text = user.DateOfBirth.ToString();
        txtEmail.Text = user.EmailAddress;
        txtName.Text = user.FirstName;
        txtPassword.Text = user.Password;
        txtSurname.Text = user.Surname;
        txtUsername.Text = user.Username;
    }
    catch (Exception ex)
    {
        txtUsername.Text = ex.Message;
    }
}
Nikolay Kostov
  • 16,433
  • 23
  • 85
  • 123
Madao
  • 7
  • 2

2 Answers2

0
  protected void Page_Load(object sender, EventArgs e)
    {
        lblUsername.Text = User.Identity.Name + "'s Profile";
        Service s = new Service();

        MyUser user = s.GetProfile(User.Identity.Name);

        try
        {
           if(user != null)
           {
            txtCountry.Text = user.Country== null? "" : user.Country.ToString();
            txtDOB.Text = user.DateOfBirth == null? "" : user.DateOfBirth.ToString();
            txtEmail.Text = user.EmailAddress== null? "" : user.EmailAddress.ToString();
            txtName.Text = user.FirstName== null? "" : user.FirstName.ToString();
            txtPassword.Text = user.Password== null? "" : user.Password.ToString();
            txtSurname.Text = user.Surname== null? "" : user.Surname.ToString();
            txtUsername.Text = user.Username== null? "" : user.Username.ToString();
          }
        }
        catch (Exception ex)
        {
            txtUsername.Text = ex.Message;
        }
    }
Sajeetharan
  • 216,225
  • 63
  • 350
  • 396
0

If you have an exception creating the command, or executing the command then it will be swallowed. You should take out the try catch in your service, or put a "throw" into the catch statement. Also the dbreader won't be disposed if there is an exception, it would be better to add a "using" around the create. I would also use a parameterized query to avoid sql injection possibility.

MyUser user = null;

using (OleDbCommand cmd = conn.CreateCommand())
{

  cmd.CommandText = @"SELECT * FROM [Details] WHERE [UserName] = @Username";
  cmd.AddParameter(new OleDbParameter('@UserName', UserName));

  //Check for matches
  using (OleDbDataReader dbReader = cmd.ExecuteReader(CommandBehavior.SingleRow))
  {

    if (dbReader.HasRows)
    {
      user = new MyUser()
      {
          FirstName = (string)dbReader["FirstName"],
          Country = (string)dbReader["Country"],
          DateOfBirth = (string)dbReader["DateOfBirth"],
          //DateOfBirth = DateTime.Parse(dbReader["DateOfBirth"].ToString()),
          EmailAddress = (string)dbReader["EmailAddress"],
          Password = (string)dbReader["Password"],
          Surname = (string)dbReader["Surname"],
          Username = (string)dbReader["UserName"]
      };
    }
  }
}  

return user;
andy_edward
  • 181
  • 1
  • 4