user1056466
user1056466

Reputation: 667

How to edit Sql query in aspx.file using asp.net

Below is my query which i want to edit in aspx file

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
     ConnectionString="<%$ ConnectionStrings:ConnectionString %>
     "InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date])
      VALUES (@ReceiptID, @EmployeeID, @Discount, @Date)">

I want to replace the @EmployeeID with a global variable value.
How can i do that. This global variable is saved in a class which I can access by global_var_session.GID.

Upvotes: 1

Views: 410

Answers (2)

Aaron Palmer
Aaron Palmer

Reputation: 9022

You can use InsertParameters:

<asp:SqlDataSource ID="SqlDataSource2" runat="server" 
 ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
 InsertCommand="INSERT INTO [Sales] ([ReceiptID], [EmployeeID], [Discount], [Date])
 VALUES (@ReceiptID, @Gid, @Discount, @Date)">

    <InsertParameters>
        <asp:Parameter Name="Gid" Type="String" />
        ...define other parameters...
    </InsertParameters>
</asp:SqlDataSource>

And in code-behind use

SqlDataSource2.InsertParameters["Gid"].DefaultValue = global_var_session.GID;
...set other parameter values...

Upvotes: 1

FeliceM
FeliceM

Reputation: 4219

I would remove the InsertCommand from the aspx and move it to the cs. Then in the .cs under the appropriate event handler:

SqlDatasource2.InsertCommand= "your query string with the reference to your Global Variable"

Upvotes: 0

Related Questions