[x]
Posted via EE Mobile

Search, ask, and monitor your questions on the go with EE Mobile. Visit Experts Exchange from your mobile device and never be out of touch again.

Question
[x]
Attachment Details
[x]
The Solution Rating System

With so many solutions, how can you tell which solutions are most likely to help you and which ones are not? To provide you with a tool to use, we rate our solutions based on various elements that most accurately determine if a solution is a quality solution. To explain what factors affect the solution rating, here are the elements we take into consideration when formulating our solution rating.

  • The Grade of the Solution
  • The Zone Rank of the Expert Providing the Solution
  • The Number of Author and Expert Comments
  • The Number of Experts Contributing
  • The Feedback of the Community

Your Input Matters
Because of the way the system is set up, the most important variable in this equation is you. As a member of Experts Exchange, you are able to cast your vote on the quality of the solutions in regard to how complete, accurate, helpful and easy to understand each solution is. When you provide your feedback, each rating is adjusted accordingly. So, if you see a solution that has a poor rating that you think is a good solution, let us know by rating it. As you do, the rating will be adjusted and will become more accurate for other members of our site.

If you have any suggestions that you would like to make for our rating system, please ask a question in the Suggestions Zone of Community Support.

Thank you!

6.4

help with getting stored procedure's return value in ASP.NET

Asked by IUAATech in Programming for ASP.NET

Tags: cast, specified, tableadapter

I have a stored proc that does a
SELECT SCOPE_IDENTITY() after inserting a record. I have tested the stored proc in query analyzer and it returns the new Key Value for the newly inserted row as expected.

In my DAL, I have a TableAdapter where one of the query is as follows (code is generated by VS2005)

        [System.Diagnostics.DebuggerNonUserCodeAttribute()]
        [System.ComponentModel.Design.HelpKeywordAttribute("vs.data.TableAdapter")]
        public virtual object CreateUser(string Afis, string UserName, string FirstName, string LastName, string Email, string PhoneNumber, string RoomNumber, System.Nullable<short> DepartmentId, System.Nullable<short> LocationId) {
            System.Data.SqlClient.SqlCommand command = this.CommandCollection[1];
            if ((Afis == null)) {
                command.Parameters[1].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[1].Value = ((string)(Afis));
            }
            if ((UserName == null)) {
                command.Parameters[2].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[2].Value = ((string)(UserName));
            }
            if ((FirstName == null)) {
                command.Parameters[3].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[3].Value = ((string)(FirstName));
            }
            if ((LastName == null)) {
                command.Parameters[4].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[4].Value = ((string)(LastName));
            }
            if ((Email == null)) {
                command.Parameters[5].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[5].Value = ((string)(Email));
            }
            if ((PhoneNumber == null)) {
                command.Parameters[6].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[6].Value = ((string)(PhoneNumber));
            }
            if ((RoomNumber == null)) {
                command.Parameters[7].Value = System.DBNull.Value;
            }
            else {
                command.Parameters[7].Value = ((string)(RoomNumber));
            }
            if ((DepartmentId.HasValue == true)) {
                command.Parameters[8].Value = ((short)(DepartmentId.Value));
            }
            else {
                command.Parameters[8].Value = System.DBNull.Value;
            }
            if ((LocationId.HasValue == true)) {
                command.Parameters[9].Value = ((short)(LocationId.Value));
            }
            else {
                command.Parameters[9].Value = System.DBNull.Value;
            }
            System.Data.ConnectionState previousConnectionState = command.Connection.State;
            if (((command.Connection.State & System.Data.ConnectionState.Open)
                        != System.Data.ConnectionState.Open)) {
                command.Connection.Open();
            }
            object returnValue;
            try {
                returnValue = command.ExecuteScalar();
            }
            finally {
                if ((previousConnectionState == System.Data.ConnectionState.Closed)) {
                    command.Connection.Close();
                }
            }
            if (((returnValue == null)
                        || (returnValue.GetType() == typeof(System.DBNull)))) {
                return null;
            }
            else {
                return ((object)(returnValue));
            }
        }

In my BLL, I have the following:

    public static short CreateUser(UserProfile u)
    {
        short userId = 0;
        try
        {
            userId = (short)usersAdapter.CreateUser(u.Afis, u.UserName, u.FirstName, u.LastName, u.Email, u.Phone, u.RoomNumber, u.Department, u.Location);
        }
        catch (Exception e)
        {
            // TODO: add logging here
        }
        return userId;
    }
    }

However, whenever the code reaches
userId = (short)usersAdapter.CreateUser(u.Afis, u.UserName, u.FirstName, u.LastName, u.Email, u.Phone, u.RoomNumber, u.Department, u.Location);

I get the following exception.

System.InvalidCastException was caught
  Message="Specified cast is not valid."
  Source="App_Code.-hk-pw4-"
  StackTrace:
       at UserProfileBLL.CreateUser(UserProfile u) in c:\Users\radhikar\Documents\Visual Studio 2005\Projects\IUAAOnline\App_Code\UserProfile\UserProfileBLL.cs:line 94

I have the query's ExecuteMode set to "Scalar" in the TableAdapter.

What am I doing wrong here? I have spent way too much time trying to figure this out.

Please help.
[+][-]03/16/07 12:06 PM, ID: 18737209Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/16/07 12:22 PM, ID: 18737333Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/16/07 12:25 PM, ID: 18737354Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/16/07 01:06 PM, ID: 18737654Expert Comment

At Experts Exchange, members can ask their questions to thousands of technology professionals, also known as Experts. Experts compete and collaborate to answer those questions by leaving comments like this one.

Start your 30-day free trial to view this Expert Comment or ask the Experts your question.

 
[+][-]03/16/07 01:07 PM, ID: 18737661Author Comment

Often, when Experts are collaborating with members who have asked questions, they will request additional information about the problem. Askers respond with an author comment like this one.

Start your 30-day free trial to view this Author Comment or ask the Experts your question.

 
[+][-]03/19/07 08:23 AM, ID: 18748670Accepted Solution

View this solution now by starting your 30-day free trial. Setting up your free trial is quick, easy, and secure. We will return you to this solution, unlocked, when you're done.

About this solution

Zone: Programming for ASP.NET
Tags: cast, specified, tableadapter
Sign Up Now!
Solution Provided By: IUAATech
Participating Experts: 1
Solution Grade: B
 
[+][-]03/19/07 03:44 PM, ID: 18752488Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
[+][-]03/24/07 09:36 AM, ID: 18785572Administrative Comment

Experts Exchange has a courteous staff of administrators who help members get the most out of the website by means of administrative comments like this one.

Start your 30-day free trial to view this Administrative Comment or ask the Experts your question.

 
 
Loading Advertisement...
20091111-EE-VQP-89