***using select query result in insert query***



 ***i have this code in .cs Page

.cs page***

        String query = "select userid from tblUser where email='" + EmplyrSession + "'";
        SqlCommand com = new SqlCommand(query, con);
        try
        {
           con.Open();
           int noRows= com.ExecuteNonQuery();
           Response.Write(noRows);
        }
        catch (Exception ex)
        {
            Response.Write("Sql Error:" + ex.Message);

        }
        finally
        {
            con.Close();
        }


   ***Now i want to use the result returned by this query i.e. userid, in .aspx page,
***
 String query = "select userid from tblUser where email='" + EmplyrSession + "'";

***i want to use the result returned by this query i.e. userid, in .aspx page, in insert command of sql datasource***

.aspx page code
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="advertisementSubmission.aspx.cs" Inherits="advertisementSubmission" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Submit JobDetails</title>
</head>
<body>
    <form id="form1" runat="server">
    <div>

        <asp:DetailsView ID="DetailsView1" runat="server" AccessKey="d" 
            AutoGenerateRows="False" DataKeyNames="advid" DataSourceID="SqlDataSource1" 
            DefaultMode="Insert" Height="50px" Width="125px" CellPadding="4" 
            ForeColor="#333333" GridLines="None">
            <FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <CommandRowStyle BackColor="#E2DED6" Font-Bold="True" />
            <RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
            <FieldHeaderStyle BackColor="#E9ECF1" Font-Bold="True" />
            <PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
            <Fields> 
                <asp:BoundField DataField="advid" HeaderText="advid" InsertVisible="False" 
                    ReadOnly="True" SortExpression="advid" />
                <asp:BoundField DataField="advtitle" HeaderText="advtitle" 
                    SortExpression="advtitle" />
                <asp:BoundField DataField="startingdate" HeaderText="startingdate" 
                    SortExpression="startingdate" />
                <asp:BoundField DataField="endingdate" HeaderText="endingdate" 
                    SortExpression="endingdate" />
                <asp:BoundField DataField="mediumofadv" HeaderText="mediumofadv" 
                    SortExpression="mediumofadv" />
                <asp:BoundField DataField="refid" HeaderText="refid" SortExpression="refid" />
                <asp:BoundField DataField="jbid" HeaderText="jbid" SortExpression="jbid" ReadOnly="True" Visible="false" />
                 <asp:TemplateField HeaderText="advDetails" SortExpression="advDetails">
                  <EditItemTemplate>
                   <asp:TextBox ID="txtboxAdvDetails" runat="server" Text='<%#Bind("advDetails") %>' TextMode="MultiLine"></asp:TextBox>
                  </EditItemTemplate>
                 </asp:TemplateField>                  
                <asp:CommandField ShowInsertButton="True" />
            </Fields>
            <HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
            <EditRowStyle BackColor="#999999" />
            <AlternatingRowStyle BackColor="White" ForeColor="#284775" />
        </asp:DetailsView>
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:Waleed_orsfinalConnectionString %>" 
            DeleteCommand="DELETE FROM [tblJobAdv] WHERE [advid] = @advid" 
            InsertCommand="INSERT INTO [tblJobAdv] ([advtitle], [startingdate], [endingdate], [mediumofadv], [refid], [jbid], [advDetails]) VALUES (@advtitle, @startingdate, @endingdate, @mediumofadv, @refid, @jbid, @advDetails)" 
            SelectCommand="SELECT * FROM [tblJobAdv]" 
            UpdateCommand="UPDATE [tblJobAdv] SET [advtitle] = @advtitle, [startingdate] = @startingdate, [endingdate] = @endingdate, [mediumofadv] = @mediumofadv, [refid] = @refid, [jbid] = @jbid WHERE [advid] = @advid">
            <DeleteParameters>
                <asp:Parameter Name="advid" Type="Int32" />
            </DeleteParameters>
            <UpdateParameters>
                <asp:Parameter Name="advtitle" Type="String" />
                <asp:Parameter DbType="Date" Name="startingdate" />
                <asp:Parameter DbType="Date" Name="endingdate" />
                <asp:Parameter Name="mediumofadv" Type="String" />
                <asp:Parameter Name="refid" Type="Int32" />
                <asp:Parameter Name="jbid" Type="Int32" />
                <asp:Parameter Name="advid" Type="Int32" />

            </UpdateParameters>

            <InsertParameters>
                <asp:Parameter Name="advtitle" Type="String" />
                <asp:Parameter DbType="Date" Name="startingdate" />
                <asp:Parameter DbType="Date" Name="endingdate" />
                <asp:Parameter Name="mediumofadv" Type="String" />
                <asp:Parameter Name="refid" Type="Int32" />          
                <asp:QueryStringParameter Name="jbid" QueryStringField="jbid" Type="Int32" />
                <asp:Parameter Name="advDetails" Type="String" />

            </InsertParameters>
        </asp:SqlDataSource>

    </div>

    </form>
</body>
</html>


*how would i get this ? HELP PLEASE *

Recommended Answers

All 7 Replies

Try this...

Place the code in the .cs page within a subrouting procedure. I see that your sub is using a response.write. Not a problem. Just call that procedure from your aspx page, where you need the results.

Like this..

<% mySub() %>

Where ever you place this code in your aspx page, the sub will be executed and place the response.write results there.

tried , but still no progress,

@mnu007, Post answers related to questions not for your own benefit , otherwise u will get deleted

Ok, so here is the most basic example of what I mean. Take a look to see if this makes sense. I have an aspx and aspx.vb page. In the vb page I have a sub that simply performs a response.write method. This sub is called from my aspx page.

aspx page:
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Default2.aspx.vb" Inherits="test_Default2" %>

<!DOCTYPE html>
<html>
<head runat="server">
    <title></title>
</head>
<body>
    <% test()%>
</body>
</html>
aspx.vb page:
Partial Class test_Default2
    Inherits System.Web.UI.Page

    Sub test()
        Response.Write("Hello World!")
    End Sub

End Class

When I run the aspx page, I get "Hello World!" displayed on the screen.

commented: very nice example jorgrm , thanks alot but you didn't get me exactly, what i want to do is that i want the result of query in my .aspx page, as u see that i called executenonquery() method to run select quey and this method only no of rows affected not th +2

i want to use the result returned by this query i.e. userid, in .aspx page, in insert command of sql datasource

Ok, sorry, re-read your question.

I havent had the need to do this myself, but I understand what you are trying to do. My first thought was assign the data you retrieve in the code behind to the text value of a HiddenField control, but that may not be necessar. A search online returned this result which appears to be very close to what you are trying to do. Take a look:
http://forums.asp.net/t/1249232.aspx/1

It shows a method of adding parameters from the code behind which is ultimately what you want to do. I thought...asign the value to a hidden control, then access that control from the data source you build on the aspx page. This referenced article just indicates to directly assign the command parameters from code behind.

good, glad to hear.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.