Welcome to Dream.In.Code
Getting Help is Easy!

Join 107,398 Programmers for FREE! Ask your question and get quick answers from experts. There are 1,201 online right now! We've got more than 500 tutorials and 2,000 snippets. Join and find out why Dream.In.Code is the #1 programming help community on the internet! Registration is fast and FREE... Join Now!



Update not working...

 
Reply to this topicStart new topic

Update not working..., But getting no error

PsychoCoder
post 2 May, 2008 - 09:26 PM
Post #1


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Ok, I have this method for updating an admin user for my site


csharp

/// <summary>
/// method for updating the users information
/// </summary>
/// <returns></returns>
public bool UpdateAdminUser()
{
try
{
//int to hold the # of rows affected
int rows = 0;
//string to hold the stored procedure we're executing
string query = "uspUpdateAdminUser";
//create a SqlConnection and open it
using (SqlConnection conn = new SqlConnection(QmetroDBHelper.GetConnectionString("Qmetro")))
{
//create a SqlCommand instance
SqlCommand cmd = new SqlCommand();
//set the commands properties
cmd.CommandText = query;
cmd.CommandType = CommandType.StoredProcedure;
//add the stored procedure properties
cmd.Parameters.AddWithValue("@intAdminUserID_PK", _userID);
cmd.Parameters.AddWithValue("@chrAccessLevel", _accessLevel);
cmd.Parameters.AddWithValue("@chrName", _name);
cmd.Parameters.AddWithValue("@chrEmail", _email);
//now see if the user changed the password
if (!(string.IsNullOrEmpty(_password)))
{
//they provided a password so we need to update that and their salt value
cmd.Parameters.AddWithValue("@chrPassword", HashPassword(_password));
cmd.Parameters.AddWithValue("@chrPasswordSalt", _saltValue);
}
else
{
//no password, pass null
cmd.Parameters.AddWithValue("@chrPassword", DBNull.Value);
cmd.Parameters.AddWithValue("@chrPasswordSalt", DBNull.Value);
}
cmd.Parameters.AddWithValue("@blnIsActive", _isActive);
cmd.Parameters.AddWithValue("@blnAccountLocked", _isLocked);
//set it's connection property
cmd.Connection = conn;

//open our connection
conn.Open();

//now execute
rows = cmd.ExecuteNonQuery();

//close our connection
conn.Close();

//clean up
conn.Dispose();
cmd.Dispose();

//check the value
if (!(rows > 0))
{
return false;
}
else
{
return true;
}
}
}
catch (SqlException ex)
{
_returnMessage = ex.Message;
return false;
}
catch (Exception ex)
{
_returnMessage = ex.Message;
return false;
}
}



I call this method in a button click


csharp

protected void cmdEditUser_Click(object sender, EventArgs e)
{
//create an Admin object
QmetroAdmin admin = new QmetroAdmin();
//set our properties
admin.FullName = edituser_fullname.Text;
admin.Email = edituser_email.Text;
admin.AccessLevel = GetSelectedSections(edituser_accessto);
admin.IsActive = Convert.ToInt32(edituser_active.SelectedItem.Value);
admin.IsLocked = Convert.ToInt32(edituser_locked.SelectedItem.Value);
admin.Password = edituser_password.Text;
admin.AdminUserID = Convert.ToInt32(Request.QueryString["id"]);

//now we update and check the status
if (!(admin.UpdateAdminUser()))
{
EditErrorMessage.Text = admin.ReturnMessage;
//Response.Redirect("Users.aspx?action=view");
}
else
{
Response.Redirect("Users.aspx?action=view");
}
}



The stored procedure works because Ive ran it in Query Analyzer with no problems. With this it is returning true but isn't updating the users information. Anyone see anything wrong? Doesn't it suck that it's generally something you've done a thousand times that ends up giving you problems
User is online!Profile CardPM

Go to the top of the page


orcasquall
post 3 May, 2008 - 07:20 AM
Post #2


D.I.C Head

Group Icon
Joined: 14 Sep, 2007
Posts: 155



Dream Kudos: 50
My Contributions


Is the value for the variable "rows" 1 (from the ExecuteNonQuery)? I'm assuming you only want to update one row. Maybe another table got affected and so the final return value is positive.

Are multiple tables updated/inserted/deleted in the stored procedure? I had this situation where I had a trigger do an insert statement. So I did something on one table, and a corresponding record was logged. Even though only 1 record was updated, there were 2 rows returned (gave me hours of fun tracking that down...).

Just throwing out ideas...
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 08:54 AM
Post #3


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Unfortunately thats not the issue here. It is updating a single table, and it has no triggers. I did find out that it will update the password if I change it, but nothing else gets updated. Rows returns 1 every time but nothing is updated (unless I provide a value for the password then the password and salt value are updated)
User is online!Profile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 08:49 PM
Post #4


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


No one else has any ideas at all?
User is online!Profile CardPM

Go to the top of the page

PsychoCoder
post 3 May, 2008 - 11:21 PM
Post #5


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


I even changed the code to check and see if 1 was returned and it still returns true, though nothing is updated in my table


csharp

/// <summary>
/// method for updating the users information
/// </summary>
/// <returns></returns>
public bool UpdateAdminUser()
{
try
{
//string to hold the stored procedure we're executing
string query = "uspUpdateAdminUser";
//create a SqlConnection and open it
using (SqlConnection conn = new SqlConnection(QmetroDBHelper.GetConnectionString("Qmetro")))
{
//create a SqlCommand instance
SqlCommand cmd = new SqlCommand();
//set the commands properties
cmd.CommandText = query;
cmd.CommandType = CommandType.StoredProcedure;
//add the stored procedure properties
cmd.Parameters.AddWithValue("@intAdminUserID_PK", _userID);
cmd.Parameters.AddWithValue("@chrAccessLevel", _accessLevel);
cmd.Parameters.AddWithValue("@chrName", _name);
cmd.Parameters.AddWithValue("@chrEmail", _email);
//now see if the user changed the password
if (!(string.IsNullOrEmpty(_password)))
{
//they provided a password so we need to update that and their salt value
cmd.Parameters.AddWithValue("@chrPassword", HashPassword(_password));
cmd.Parameters.AddWithValue("@chrPasswordSalt", _saltValue);
}
else
{
//no password, pass null
cmd.Parameters.AddWithValue("@chrPassword", DBNull.Value);
cmd.Parameters.AddWithValue("@chrPasswordSalt", DBNull.Value);
}
cmd.Parameters.AddWithValue("@blnIsActive", _isActive);
cmd.Parameters.AddWithValue("@blnAccountLocked", _isLocked);
//set it's connection property
cmd.Connection = conn;

//open our connection
conn.Open();

//now execute
int rows = Convert.ToInt32(cmd.ExecuteNonQuery());

//clean up
conn.Dispose();
cmd.Dispose();

//check the value
if (!(rows == 1))
{
return false;
}
else if (rows == -1)
{
return false;
}
else
{
return true;
}
//close our connection
conn.Close();
}
}
catch (SqlException ex)
{
_returnMessage = ex.Message;
return false;
}
catch (Exception ex)
{
_returnMessage = ex.Message;
return false;
}
}
User is online!Profile CardPM

Go to the top of the page

baavgai
post 4 May, 2008 - 04:17 AM
Post #6


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,524



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


The code looks fine. Except for all the global variables; they kill kittens, you know? wink2.gif

And will that, one place it could be screwing up is if you're passing nulls that you weren't expecting.

Also, the int value returned from a stored procedure is not necessarily the number of rows affected, it's more a status. Unless you are explicitly returning something like rows affected, it will probably be 1 as long as no errors are raised.
User is offlineProfile CardPM

Go to the top of the page

PsychoCoder
post 4 May, 2008 - 07:32 AM
Post #7


DIC.Rules == true;

Group Icon
Joined: 26 Jul, 2007
Posts: 7,135



Thanked 50 times

Dream Kudos: 7700

Expert In: VB, VB.Net, C#, SQL, ASP, ASP.Net, Web Development, HTML, CSS, Win32 API, Javascript, mySQL, J#, GDI

My Contributions


Those are property variables. I always use those instead of the properties themselves, just the way I was "taught" to do it. In all ExecuteNonQuery() methods rows returns the number of rows affected, Ive always used it and it has never failed me. The 2 I am passing null's to, the stored procedure looks for them as they are not required.

Now if I pass a password (then the salt is generated of course) those 2 values, password and salt, are updated in the table but nothing else is. If I don't update the password nothing is updated

From MSDN
QUOTE

For UPDATE, INSERT, and DELETE statements, the return value is the number of rows affected by the command. When a trigger exists on a table being inserted or updated, the return value includes the number of rows affected by both the insert or update operation and the number of rows affected by the trigger or triggers. For all other types of statements, the return value is -1. If a rollback occurs, the return value is also -1.
User is online!Profile CardPM

Go to the top of the page

baavgai
post 4 May, 2008 - 08:55 AM
Post #8


Dreaming Coder

Group Icon
Joined: 16 Oct, 2007
Posts: 1,524



Thanked 41 times

Dream Kudos: 325

Expert In: C, C++, Java, C#, ASP.NET, PHP, Perl, Python, Oracle, SQL Server, MySql, HTML, JavaScript, Lua

My Contributions


I'm sorry I couldn't help. Whether you choose to believe me or not, I still think I'm correct on stored procedure return value. "UPDATE, INSERT, and DELETE statements" are not stored procedures.

http://msdn.microsoft.com/en-us/library/aa174792.aspx
QUOTE

Microsoft SQL Server stored procedures return data in four ways...
...
Return codes, which are always an integer value.
...
Return codes are generally used to pass back error checking information.



http://msdn.microsoft.com/en-us/library/ms378371.aspx
QUOTE

A SQL Server stored procedure that you can call is one that returns a status or a result parameter. This is typically used to indicate the success or failure of the stored procedure.


Good luck.
User is offlineProfile CardPM

Go to the top of the page

skaoth
post 4 May, 2008 - 10:39 AM
Post #9


D.I.C Regular

Group Icon
Joined: 7 Nov, 2007
Posts: 319



Thanked 5 times

Dream Kudos: 100
My Contributions


I don't see anything wrong in your code either. I've been looking at MSDN and
the only thing different between the sample code there and yours
is passing in the connection object to the command object at instantiation

MSDN
CODE

private static void CreateCommand(string queryString,
    string connectionString)
{
    using (SqlConnection connection = new SqlConnection(
               connectionString))
    {
        SqlCommand command = new SqlCommand(queryString, connection);
        command.Connection.Open();
        command.ExecuteNonQuery();
    }
}

I doubt that that could be it though
User is offlineProfile CardPM

Go to the top of the page

orcasquall
post 5 May, 2008 - 05:38 AM
Post #10


D.I.C Head

Group Icon
Joined: 14 Sep, 2007
Posts: 155



Dream Kudos: 50
My Contributions


Ok, throwing out more ideas...

1) Is your update statement a simple one? Something complex like
sql

update sometable
set col1 = B.col1
col2 = B.col2
from sometable A, anothertable B
where A.col3 = B.col3

might update more rows than you thought... and the row you wanted updated didn't get updated (though your test in Query Analyser should debunk this...)

2) I assume the variables with an underscore prefix are member variables of the QmetroAdmin class? Perhaps one of the get/set retrieved/assigned some values you didn't expect? Like baavgai mentioned, an unexpected null somewhere... Can check if the values passed in are the values you expected.

3) "update" access for the table and/or "exec" access for the stored proc granted? You probably ran as sa in QA. Maybe the web app connection ID didn't have access... and failed silently... (though why only the password is updated is weird... just throwing out ideas!)

4) Default values for the table columns? Might explain the weirdness in 3)

5) The where clause in the update statement in the stored proc? An incorrect formulation so a row was updated, just not the one you wanted?

6) Data problems? A duplicate row somewhere? Maybe you can keep only one row and see if that works out...

7) Wrong table or stored proc used (or even in the wrong database)? It happens... tongue.gif
User is offlineProfile CardPM

Go to the top of the page

Fast ReplyReply to this topicStart new topic
Time is now: 8/28/08 04:12PM

Live Help!

Tutorials

Programming

Web Development

Reference Sheets

Code Snippets

Bye Bye Ads

Free DIC T-Shirt

T-Shirt Example

Related Sites

Monthly Drawing

Thumb Drive

Partners

Top Contributors

Top 10 Kudos This Month