Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Radio button SQL query from C# web site

Posted on 2009-05-13
3
Medium Priority
?
778 Views
Last Modified: 2013-12-17
Hello expert,

Created a simple page to do twodifferent queries based on a radio button selection.
Checked the queries by running in Query Analyzer with positive results
Don't get any compiler or run time errors in the browser but don't get any data.
Using VS 2005.
Can you tell whats wrong please?
Code below..
++++++++ begin default.aspx +++++++++++++
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
 
<!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 id="Head1" runat="server">
    <title>WTE Toolkit 2009 RB Test</title>
    <link rel="stylesheet" type="text/css" href="TK.css" />
</head>
<body>
 
<div style="LEFT: 20px; WIDTH: 654px; POSITION: absolute; TOP: 16px; ">
    <form id="frmToolKit" runat="server">
<asp:RadioButton id="sm" Text="sm"  
GroupName="query" runat="server"/>
<br />
<asp:RadioButton id="jo" Text="jo"
GroupName="query" runat="server"/><br />
 
<asp:Button ID="btnSearch" Text="Search" runat="server" />
 
<br />
    <asp:GridView ID="grdPayroll" runat="server" AllowPaging="true" AllowSorting="true"
        PageSize="15" DataSourceID="dsToolkit" AutoGenerateColumns="false">
        <Columns>
            
            <asp:BoundField DataField="Username" HeaderText="Username" SortExpression="Username"
                HeaderStyle-Width="10%" />
              <asp:BoundField DataField="First_Name" HeaderText="First_Name" SortExpression="First_Name"
                HeaderStyle-Width="10%" />  
            <asp:BoundField DataField="Last_Name" HeaderText="Last_Name" SortExpression="Last_Name"
                HeaderStyle-Width="10%" />   
            <asp:BoundField DataField="Company_Number" HeaderText="Company" SortExpression="Company"
                HeaderStyle-Width="10%" />
        </Columns>
    </asp:GridView>
    <asp:SqlDataSource ID="dsToolkit" runat="server"></asp:SqlDataSource>
  
    </form></div>
</body>
</html>
 
 
++++++++ end default.aspx +++++++++++++
 
++++++++ begin default.aspx.cs +++++++++++++
 
using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
 
public partial class _Default : System.Web.UI.Page
{
    String sql = String.Empty; 
 
     
    protected void btnSearch_Click(object sender, EventArgs e)
    {
        if (sm.Checked)
            sql = "SELECT Username, First_Name, Last_Name, Company_Number FROM tblSecurity WHERE (Username LIKE '%sm%')";
        if (jo.Checked)
 
            sql = "SELECT Username, First_Name, Last_Name, Company_Number FROM tblSecurity WHERE (Username LIKE '%jo%')";
        String connString = "DATA SOURCE = Labsdirvg600;Initial Catalog=TimeEntry;User ID=Labswirvg600;Password=Kling0n1";
 
        //Set the connection string of the SQLDataSource here
        dsToolkit.ConnectionString = connString;
 
        //Now, the dynamically created SQL statement needs to be assigned to the SQL Data source
        dsToolkit.SelectCommand = sql;
 
        //Finally, refresh the Grid, which will cause the data source to execute the above created
        //SQL and show the results in the grid
        grdPayroll.DataBind();
    }
 
}
 
++++++++ end default.aspx.cs +++++++++++++

Open in new window

0
Comment
Question by:Allen Pitts
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 2

Assisted Solution

by:kavint
kavint earned 800 total points
ID: 24380128
You have to set the Grid's datasource to dsToolKit before you do a DataBind(), like this

grdPayroll.DataSource = dsToolkit;
grdPayroll.DataBind();

It would be even better to have the above lines in a callable function.
0
 
LVL 20

Accepted Solution

by:
ddayx10 earned 1200 total points
ID: 24380200
A little hard to say without actually hooking it up to a db of my own, but the first thing that came accross for me was that no postback was occurring on the button click, so I changed the button to this:

<asp:Button ID="btnSearch" Text="Search" runat="server" OnClick="btnSearch_Click" />

I was a little concerned with your if statements from lines 65-69. This is semi-acceptable shorthand, but can at times be problematic if you aren't careful (I checked and it seemed to work out though). You might want to just assure that the syntax is safe by seperating that a bit and using the obligatory {}'s.

Otherwise if that doesnt do it, I strongly suggest putting a break there and debugging it to follow the logic.

If still no mojo try removing the datasource references from the front end and put it all on the backside:

sqldatasource ds = new sqldatasource(connString)
        if (sm.Checked)
            {ds.selectcommand = "SELECT Username, First_Name, Last_Name, Company_Number FROM tblSecurity WHERE (Username LIKE '%sm%')";}
        if (jo.Checked){
             ds.selectcommand ="SELECT Username, First_Name, Last_Name, Company_Number FROM tblSecurity WHERE (Username LIKE '%jo%')";}

grdpayroll.datasourceID = ds.id  //you may not need this
grdpayroll.datasource = ds
grdpayroll.databind()

Hope this helps somewhat, good luck.

dday
0
 
LVL 22

Expert Comment

by:8080_Diver
ID: 24380278
If it is a problem in the queries, you can debug it by running the following three queries in a query tool.   First, run SQL_1 and see what the data in the Username column looks like.  Then try SQL_2.  If SQL_1's results in the Username column were all caps, try SQL_3.  (If the SQL_1 results for the Username were "camelcase", also try changing the WHERE clause in SQL_3 to look for '%Sm%'.)
You may have a Case Sensitive collation set on the database and the data may be in all caps or camel case.

SQL_1:
SELECT TOP 10
       Username, 
       First_Name, 
       Last_Name, 
       Company_Number 
FROM tblSecurity;
 
 
SQL_2:
SELECT Username, 
       First_Name, 
       Last_Name, 
       Company_Number 
FROM tblSecurity 
WHERE (Username LIKE '%sm%');
 
 
SQL_3:
SELECT Username, 
       First_Name, 
       Last_Name, 
       Company_Number 
FROM tblSecurity 
WHERE (Username LIKE '%SM%');

Open in new window

0

Featured Post

Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Welcome my friends to the second instalment and follow-up to our Minify and Concatenate Your Scripts and Stylesheets (http://www.experts-exchange.com/Programming/Languages/.NET/ASP.NET/A_4334-Minify-and-Concatenate-Your-Scripts-and-Stylesheets.html)…
For those of you who don't follow the news, or just happen to live under rocks, Microsoft Research released a beta SDK (http://www.microsoft.com/en-us/download/details.aspx?id=27876) for the Xbox 360 Kinect. If you don't know what a Kinect is (http:…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…
In this video, Percona Solution Engineer Dimitri Vanoverbeke discusses why you want to use at least three nodes in a database cluster. To discuss how Percona Consulting can help with your design and architecture needs for your database and infras…

715 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question