Select SQL query column

Discussion in 'ASP .Net' started by Stephan Bour, Nov 3, 2003.

  1. Stephan Bour

    Stephan Bour Guest

    I need to implement a simple search function to lookup order information in
    a SQLserver table. The code below works fine for a search to the ³OrderID²
    column. However, I¹d like to offer the user the ability to select 3 possible
    columns to search from. I added a dropdownlist to select the column. My
    question is, how do I dynamically modify the SQL command <String selectCmd =
    "select * from Orders where OrderID = @OrderID";> to take the input from the
    dropdownlist?
    Also, can I assign a different Value to <asp:listitem>Order
    ID</asp:listitem> than ³Order ID²? I¹d like to display the parameter with a
    space but have its Value = OrderID (no space) since that¹s the column name
    in the SQL table.
    Thank you,
    Stephan.


    <%@ Page Language="C#" ContentType="text/html" ResponseEncoding="iso-8859-1"
    %>
    <%@ Import Namespace="System.Data" %>
    <%@ Import Namespace="System.Data.SqlClient" %>

    <html>
    <script language="C#" runat="server">

    public void GetOrder(Object sender, EventArgs E)
    {
    String selectCmd = "select * from Orders where OrderID = @OrderID";

    SqlConnection myConnection = new SqlConnection("server =
    'LMM-MB-DATA,25886';Database = 'Oligo_Order'; Trusted_Connection=Yes");
    SqlDataAdapter myCommand = new SqlDataAdapter(selectCmd,
    myConnection);

    myCommand.SelectCommand.Parameters.Add(new SqlParameter("@OrderID",
    SqlDbType.NVarChar));
    myCommand.SelectCommand.Parameters["@OrderID"].Value =
    SearchString.Text;

    DataSet ds = new DataSet();
    myCommand.Fill(ds, "Orders");

    MyDataGrid.DataSource= ds.Tables["Orders"].DefaultView;
    MyDataGrid.DataBind();
    }

    </script>

    <body style="font: 10pt verdana">

    <form runat="server">
    <p> Search previously placed order by:
    <asp:dropdownlist ID="SearchSelect" runat="server">
    <asp:listitem>Order ID</asp:listitem>
    <asp:listitem>Last name</asp:listitem>
    <asp:listitem>Comments</asp:listitem>
    </asp:dropdownlist>
    <asp:textbox id="SearchString" MaxLength="50" runat="server"
    TextMode="SingleLine" />
    </p>

    <input type="submit" OnServerClick="GetOrder" Value="Get Orders"
    runat="server"/><p>

    <ASP:DataGrid id="MyDataGrid" runat="server"
    Width="700"
    BackColor="#ccccff"
    BorderColor="black"
    ShowFooter="false"
    CellPadding=3
    CellSpacing="0"
    Font-Name="Verdana"
    Font-Size="8pt"
    HeaderStyle-BackColor="#aaaadd"
    EnableViewState="false"
    />

    </form>

    </body>
    </html>
     
    Stephan Bour, Nov 3, 2003
    #1
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Anonymous
    Replies:
    0
    Views:
    1,475
    Anonymous
    Oct 13, 2005
  2. Greg Smith
    Replies:
    0
    Views:
    542
    Greg Smith
    Apr 12, 2007
  3. asgars
    Replies:
    1
    Views:
    2,336
    Patrice
    May 11, 2010
  4. Greg Smith
    Replies:
    0
    Views:
    161
    Greg Smith
    Apr 12, 2007
  5. palmiere
    Replies:
    1
    Views:
    413
    Erwin Moller
    Feb 9, 2004
Loading...

Share This Page