Updating is nor working

  • Thread starter Thread starter F.oliveirarocha
  • Start date Start date
F

F.oliveirarocha

Guest
Hi folks, good evening,

I'm trying to do an Update, and it is nort working, the method insert is almost the same and it is working properly, with no errors.

The method Update does not execute the update on the table. I can't figure out what's going on, because the breakpoint in the visual studio, does not stop where the point is marked, something weird is happening.

Follows bellow the Insert code, Update code and the table.

I appreciate all helpers. Thanks for all of you.

#region Methods of Insert.

private bool cadastrarUser(User users)
{
CultureInfo provider = CultureInfo.InvariantCulture;
provider = new CultureInfo("pt-BR");

try
{
StringBuilder query = new StringBuilder();
query.AppendFormat("insert into Users(username, loginname, [password], [date], userlogged, datelogged)");
query.AppendFormat(" VALUES (@UserName, @loginname, @password, @Date, @userlogged, @datelogged)");

connectionOpen();

using (SqlCommand cmd = connection.CreateCommand())
{

cmd.CommandType = CommandType.Text;
cmd.CommandText = query.ToString();
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@UserName", users.username));
cmd.Parameters.Add(new SqlParameter("@loginname", users.loginname));
cmd.Parameters.Add(new SqlParameter("@password", users.password));
cmd.Parameters.Add(new SqlParameter("@Date", users.date));
cmd.Parameters.Add(new SqlParameter("@userlogged", users.userlogged));
cmd.Parameters.Add(new SqlParameter("@datelogged", users.datelogged));
int result = cmd.ExecuteNonQuery();
cmd.Dispose();

int resultado = result;

return true;
}
}
catch (Exception ex)
{
return false;
}
finally
{
connectionClose();
}
}
#endregion Methods of Insert.



Method of Update

private bool alterarUser(User users, int userID)
{
CultureInfo provider = CultureInfo.InvariantCulture;
provider = new CultureInfo("pt-BR");

try
{
StringBuilder query = new StringBuilder();
query.AppendFormat("UPDATE Users SET username=@username, loginname=@loginname, [password]=@password, [date]=@Date, userlogged=@userlogged, datelogged=@datelogged WHERE userID=@id");

connectionOpen();

using (SqlCommand cmd = connection.CreateCommand())
{
cmd.CommandType = CommandType.Text;
cmd.CommandText = query.ToString();
cmd.Parameters.Clear();
cmd.Parameters.Add(new SqlParameter("@UserName", users.username));
cmd.Parameters.Add(new SqlParameter("@loginname", users.loginname));
cmd.Parameters.Add(new SqlParameter("@password", users.password));
cmd.Parameters.Add(new SqlParameter("@Date",users.date));
cmd.Parameters.Add(new SqlParameter("@userlogged", users.userlogged));
cmd.Parameters.Add(new SqlParameter("@datelogged", users.datelogged));
cmd.Parameters.Add(new SqlParameter("@id", users.userID));
connection.Open();
int result =cmd.ExecuteNonQuery(); // the breakpoint doesn't stop here
cmd.Dispose();

int resultado = result; // the breakpoint doesn't stop here either

return true;
}
}
catch (Exception )
{
return false;
}
finally
{
connectionClose();
}
}


Table - SQLServer 2014

userID int Unchecked
username varchar(50) Checked
loginname varchar(25) Checked
password varchar(25) Checked
date date Checked
userlogged varchar(25) Checked
datelogged date Checked

Continue reading...
 
Back
Top