Kumi
Kumi

Reputation: 1

Searching through Database - in asp c# Visual Web Developer Express

I would like to search for a specific number via user input... this si not working at the moment, however even when i hardcode a number it still does not reteive anything. I am not sure what i am doing wrong. Please assist me, it is rather urgent.

Tahnk you you, the files are below.

cs file

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;
using System.Data.SqlClient;

namespace SearchC
{
    public partial class _Default : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }

        protected void SearchClick(object sender, EventArgs e)
        {
            string searchPAS = txtSearch.Text;
            using (var sqlconnection = new SqlConnection(Search)
            {
                string strQuery = "Select PAS_NO from P_DETAILS where PAS_NO = searchPAS";
                SqlCommand command = new SqlCommand(strQuery, sqlconnection) { CommandType = CommandType.Text };
                var ptnt = command.ExecuteScalar();
                txtPAS.Text = ptnt.ToString();
            }

        }
    }
}

Web config file

<configuration>
  <connectionStrings>
    <add name="Search" connectionString="Data Source=dwh;User ID=***;Password=***;Unicode=True"
        providerName="System.Data.OracleClient" />
  </connectionStrings>

  <system.web>
    <compilation debug="true" targetFramework="4.0" />

    <authentication mode="Forms">
      <forms loginUrl="~/Account/Login.aspx" timeout="2880" />
    </authentication>

    <membership>
      <providers>
        <clear/>
        <add name="AspNetSqlMembershipProvider" type="System.Web.Security.SqlMembershipProvider" connectionStringName="ApplicationServices"
             enablePasswordRetrieval="false" enablePasswordReset="true" requiresQuestionAndAnswer="false" requiresUniqueEmail="false"
             maxInvalidPasswordAttempts="5" minRequiredPasswordLength="6" minRequiredNonalphanumericCharacters="0" passwordAttemptWindow="10"
             applicationName="/" />
      </providers>
    </membership>

    <profile>
      <providers>
        <clear/>
        <add name="AspNetSqlProfileProvider" type="System.Web.Profile.SqlProfileProvider" connectionStringName="ApplicationServices" applicationName="/"/>
      </providers>
    </profile>

    <roleManager enabled="false">
      <providers>
        <clear/>
        <add name="AspNetSqlRoleProvider" type="System.Web.Security.SqlRoleProvider" connectionStringName="ApplicationServices" applicationName="/" />
        <add name="AspNetWindowsTokenRoleProvider" type="System.Web.Security.WindowsTokenRoleProvider" applicationName="/" />
      </providers>
    </roleManager>

  </system.web>

  <system.webServer>
     <modules runAllManagedModulesForAllRequests="true"/>
  </system.webServer>
</configuration>

aspx file

<%@ Page Title="Home Page" Language="C#" MasterPageFile="~/Site.master" AutoEventWireup="true"
    CodeBehind="Default.aspx.cs" Inherits="SearchC._Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
</asp:Content>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">

    <table> 
            <tr> 
                <td> 
                    PAS NUmber
                </td> 
                <td> 
                     <asp:TextBox ID="txtSearch" runat="server"></asp:TextBox> 
                </td> 
            </tr> 
            <tr > 

                <td > 
                     <asp:Button runat="server" ID="btnSearch" OnClick="SearchClick" Text="Search"/> 
                </td> 
            </tr> 
             <tr > 

                <td > 
                     <asp:TextBox ID="txtPAS" runat="server"></asp:TextBox> 
                </td> 
            </tr> 
        </table>


</asp:Content>

This is what i have now... am unsure why it is not working, it just flashes when i press the button and nothing else happens

Upvotes: 0

Views: 407

Answers (1)

Forte L.
Forte L.

Reputation: 2812

You are searching for the string searchPAS instead of what the variable contains. Change your query like this to use parameters:

string strQuery = "Select PAS_NO from P_DETAILS where PAS_NO = @searchPAS";
SqlCommand command = new SqlCommand(strQuery, sqlconnection) { CommandType = CommandType.Text };
command.Parameters.AddWithValue("searchPAS", searchPAS);
var ptnt = command.ExecuteScalar();
txtPAS.Text = ptnt.ToString();

Upvotes: 1

Related Questions