In my C# Web Application I have simple page where a user enters their first name in one textbox (named txtFirst) and their last name in another textbox (named txtLast). After they enter this information in the two textboxes, they click on a continue button which saves the data to a SQL database. My problem is trying to save these values in SQL. I have a “Value” table (columns are PersonID, AttributeID, and Value) and an “Attribute” table (columns are a primary key AttributeID and AttributeName).
In my Attribute table I have the following:
AttributeID: AttributeName:
1 FirstName
2 LastName
My goal is to have this save in the Value table as:
PersonID: AttributeID: Value:
A 1 FirstName Value Here
B 2 LastName Value Here
I know how to save the data by directly passing it into a stored procedure, but how can I have the AttributeID get assigned correctly? (Example: AttributeID = 1 for FirstName and AttributeID = 2 for LastName). Is there a way I can assign the textboxes in the application to have a specific AttributeID or even by AttributeName? Thanks so much.
It might be confusing, but the idea here is for when the button is clicked, it pulls the AttributeID associated to FirstName from the table and then saves the value and that pulled AttributeID in table “Value”. Thanks
I would store them in the
ViewStateorSessiondepending on what you are doing:Then to access just use
Session("FirstNamePersionId")or what ever you called the value.I would wrap the calls to the
ViewStateorSessionin properties to make it easier to maintain and read.Let me know if you need an example.
Update:
To get the values from the database you can try the following.
If you want you can make a separate call for each id, but that adds a bit of processing overhead. If you do make two calls then I suggest using the same
SqlConnection, though you will need to create a new command.Accessing the results by id (as I did using
sqlReader[1]) is sometimes frowned upon. I personally do not worry about it since I declare the column order in the SQL query.This would probably be easier using LINQ2SQL.
Let me know if I made any mistakes or missed anything.