Adding Automated Fields In ASP.NET

I'm sure this is a basic question:
I have created a stored procedure to input a new row into a database table. The user only inputs 4 fields, and I have 3 more fields that are automatically updated.

2 of these fields are the UserID and ClientID.
UserID is a foreign key (from the aspnetdb) and ClientID is this tables primary key.

My question is:
How do I input the current UserID and the ClientID into the database automatically with the stored procedure and/or c#

I'm using C# in Visual Studio 2008 and SQL.
If I am going about this the wrong way all together, feel free to let me know.

Thanks
richgnAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Carl TawnSystems and Integration DeveloperCommented:
Are you wanting to pass the UserID and ClientID to the stored procedure or wanting to do it from the SP itself? You will have a hard time doing it from the SP end becaese the database has not concept of a current user in ASPNETDB terms.
0
richgnAuthor Commented:
Whichever way works best,
I guess passing the UserID and ClientID to the SP sounds best.
Do you know how I would do that in C#?
0
Carl TawnSystems and Integration DeveloperCommented:
If ClientID is an Identity column then it will be populated automatically when you insert a new record to the table.

You can get the GUID of the current AspNet user using:

     System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();

So you can pass this to your SP to identify the user, then you can just store that to make the link to the aspnet user.
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

richgnAuthor Commented:
I've added that in, thanks, but seem to be getting another problem now- here is my code. Any ideas? If I'm doing anything completely wrong please let me know.

Thanks!

       SqlConnection Conn = new     SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString);
        SqlCommand cmd = new SqlCommand("CreateNewClient", Conn);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlParameter ClientName = cmd.Parameters.Add("ClientName", txt_ClientName.Text);
        System.Web.Security.Membership.GetUser().ProviderUserKey.ToString();
        PortfolioName.Direction = ParameterDirection.ReturnValue;
        Conn.Open();
        SqlDataReader myReader = cmd.ExecuteReader();

The error I get is:
Warning    1    'System.Data.SqlClient.SqlParameterCollection.Add(string, object)' is obsolete: 'Add(String parameterName, Object value) has been deprecated.  Use AddWithValue(String parameterName, Object value).  http://go.microsoft.com/fwlink/?linkid=14202'
0
Carl TawnSystems and Integration DeveloperCommented:
That message is only a warning rather than an error. It is simply telling you that the method you are using is deprecated, measning it has been replaced with a newer version in the framework.

However, your code doesn't look quite right, you probably want something more along the lines of:
SqlConnection Conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionStr"].ConnectionString);

SqlCommand cmd = new SqlCommand("CreateNewClient", Conn);
cmd.CommandType = CommandType.StoredProcedure;

cmd.Parameters.AddWithValue("ClientName", txt_ClientName.Text);
cmd.Parameters.AddWithValue("UserID", System.Web.Security.Membership.GetUser().ProviderUserKey.ToString());

// i assume this parameter is being added somewhere else
PortfolioName.Direction = ParameterDirection.ReturnValue;

Conn.Open();
SqlDataReader myReader = cmd.ExecuteReader();

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
richgnAuthor Commented:
Thanks! That works like a charm,

I just have one more question that isn't part of the question (just more for my curiosity).
If the UserID was not part of the aspnetdb and did not need to be secure (ie. Just a foreign key with a value in another table)

What would be the best way to call it?

Thanks again hey!!
0
Carl TawnSystems and Integration DeveloperCommented:
That depends on your application. If you were not using AspNet membership then presumably you would still have a user authentication mechanism and the concept of a current user.

So in that case you would have to keep track of the "current" user in your particualr context and pass the ID from that.
Odds are that you would have some sort of User object stored in the session to represent the currently logged on user, and that object would persist for the users entire session.
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
ASP.NET

From novice to tech pro — start learning today.