Showing posts with label selection. Show all posts
Showing posts with label selection. Show all posts

Friday, March 23, 2012

Finding "Id" with "Username" selection and then Inserting

I have a simple insert statement that takes values from textboxes and inserts them into a sql server database. I would like to add a value that is selected from a dropdown list and find its corresponding unique "Id" value and insert into the database as well.

My sql statement looks like this:

string strSQL = "INSERT INTO aspnet_Expenses (ExpenseDate,RentalCar,ect..) VALUES (@.ExpenseUserId,@.ExpenseDate,@.RentalCar,ect..)";

I would like to add this to it: SELECT @.ExpenseUserId = UserId FROM aspnet_users WHERE Username = ExpenseUserName

1) How do I assign the value from the dropdown list to save as the "ExpenseUserName"

and

2) Am I on the right path with this at all?

To do this in the least number of trips to the DB, pass all the parameters you have to a stored proc. In your proc:

(1) Use the SELECT from aspnet_users to get the userid into a variable.

(2) Do your regular insert.

|||

How do I pass the values to a stored procedure? Will I need to create a blll and data access layer? I've tried learning those but I was hoping to do something a little more straight forward. I'm pretty new to all of this.

|||OK, you can start from here:Using Stored Procedures with a Command
Try it and if you have any trouble please feel free to post it.
sql

Sunday, February 19, 2012

Filtering Table name with Parameter

<br><br>I obtain table names from a database and pass them to a dropdownlist. Based on user selection, I want to pass each table name to a query.Here is an extract from my code: <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>" SelectCommand="select * from @.dDTable"> <SelectParameters> <asp:ControlParameter ControlID="DropDownList1" Name="dDTable" PropertyName="SelectedValue" DefaultValue="product" Direction="InputOutput" Size="15" Type="String" /> </SelectParameters> </asp:SqlDataSource>I keep getting this error: Must declare the table variable "@.dDTable".Please does anyone knows the best way to go about this?

Hi,

thats not working. Dynamic table name are allowed in here. You could use a stored procedure instead which then uses dynamic sql to execute the query that was first concatenated (with the table name).

But you should be aware of the curse of dynamic SQL :-)

http://www.sommarskog.se/dynamic_sql.html

HTH, Jens Suessmeyer.

http://www.sqlserver2005.de

|||Thanks.