2

I'm creating ASP.NET, C# application. Where are two textboxes when typing value result is shown in gridview from SQL server. Problem is that when i type number 5 i getting 15 value, example. Can someone give an hint?

How to get exactly value from database as typed in textbox?

Source Code:

SQL Procedure

Create table Hall
(
    HallID int,
    Name nvarchar(50),
    TicketLimit int
)
GO

Create table HallGroup
(
    HallID int,
    HallGroupID int,
    Name nvarchar(50),
    AZ int
)
GO

Create table HallSeat
(
    HallGroupID int,
    ShowSeatID int,
    Color nvarchar(15),
    Price int,
    SeatRow int,    
    SeatNumber int, 
    IsReserved bit
)

/**Search Procedure ****/

USE [Reservations]
GO

SET ANSI_NULLS ON
GO
SET QUOTED_IDENTIFIER ON
GO
ALTER Procedure [dbo].[Search]
@searchNumber varchar(50),
@searchRow varchar(50)

as
Begin   
    select * from HallSeat where (SeatNumber like '%' + @searchNumber + '%') and (SeatRow  like '%' + @searchRow + '%')

End

Code Behind

protected void ButtonSearch_Click(object sender, EventArgs e)
    {
        cs = ConfigurationManager.ConnectionStrings["CS"].ConnectionString;

        SqlConnection result = new SqlConnection(cs);

        String search = "Search"; // Paieskos         
        SqlCommand find = new SqlCommand(search, result);
        find.CommandType = CommandType.StoredProcedure;
        find.Parameters.Add("@searchRow", SqlDbType.VarChar).Value = TextBoxRow.Text;
        find.Parameters.Add("@searchNumber", SqlDbType.VarChar).Value = TextBoxNumber.Text;

        result.Open();
        SqlDataReader dr;
        dr = find.ExecuteReader();

        if (dr.HasRows)
        {
            dr.Read();

            rep_bind();
            GridView1.Visible = true;

            TextBoxRow.Text = "";
            TextBoxNumber.Text = "";
        }
        else
        {
            GridView1.Visible = false;

        }

    }

Aspx

<div class="container">      

        <asp:TextBox ID="TextBoxRow" placeholder="Type Seat Row" runat="server" style="border-radius: 5px;" Height="35px"></asp:TextBox>             
        <asp:TextBox ID="TextBoxNumber" placeholder="Type Seat Number" runat="server" style="border-radius: 5px;" Height="35px"></asp:TextBox>
        <br>
        <br>
        <br>

        <asp:Button ID="ButtonSearch" runat="server" CssClass="button" Text="Search for availability" OnClick="ButtonSearch_Click" OnClientClick="true" UseSubmitBehavior="false"  />
        <style type="text/css">
            .button {
              background:white;
              color: black;
              border: solid 3px #6496c8;
              font-family: cursive;
              font-size: 18px;
            }
            .button:hover,
            .button.hover
            {
              border-color: #346392;
              background: #7ABCDC;
              color: white;            
            }
            .button:active{
              border-color: #27496d;
              color: #27496d;
            }

        </style>
    </div>

    <!-- Paieskos rezultatu isvedimas GridView-->

 <div id="GRID">
   <asp:GridView ID="GridView1" runat="server"  HorizontalAlign="Center" AllowPaging="True" CellPadding="3" Width="568px"  GridLines="Vertical" Visible="False" AutoGenerateColumns="False" BackColor="White" BorderColor="#999999" BorderStyle="None" BorderWidth="1px">       

        <FooterStyle BackColor="#CCCCCC" ForeColor="Black" />
        <HeaderStyle BackColor="#121315" Font-Bold="True" ForeColor="White" />
        <PagerStyle BackColor="#999999" ForeColor="Black" HorizontalAlign="Center" />
        <RowStyle BackColor="#EEEEEE" ForeColor="Black" />
        <SelectedRowStyle BackColor="#008A8C" Font-Bold="True" ForeColor="White" />
        <SortedAscendingCellStyle BackColor="#F1F1F1" />
        <SortedAscendingHeaderStyle BackColor="#0000A9" />
        <SortedDescendingCellStyle BackColor="#CAC9C9" />
        <SortedDescendingHeaderStyle BackColor="#000065" />  

           <AlternatingRowStyle BackColor="#DCDCDC" />

      <Columns>          

         <asp:TemplateField HeaderText="HALL">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox1" runat="server" Text='<%# Bind("HallGroupID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label1" runat="server" Text='<%# Bind("HallGroupID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>


          <asp:TemplateField HeaderText="ID">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label3" runat="server" Text='<%# Bind("ShowSeatID") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="COLOR">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox4" runat="server" Text='<%# Bind("Color") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label4" runat="server" Text='<%# Bind("Color") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>

           <asp:TemplateField HeaderText="PRICE">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox5" runat="server" Text='<%# Bind("Price") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label5" runat="server" Text='<%# Bind("Price") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>  

          <asp:TemplateField HeaderText="ROW">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label6" runat="server" Text='<%# Bind("SeatRow") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField> 

          <asp:TemplateField HeaderText="NUMBER">
                <EditItemTemplate>
                    <asp:TextBox ID="TextBox7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:TextBox>
                </EditItemTemplate>
                <ItemTemplate>
                    <asp:Label ID="Label7" runat="server" Text='<%# Bind("SeatNumber") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>               

           <asp:TemplateField HeaderText="STATUS" SortExpression="Active">
                    <ItemTemplate><%# (Boolean.Parse(Eval("IsReserved").ToString())) ? "RESERVED" : "FREE" %></ItemTemplate>
             </asp:TemplateField>            

            <asp:TemplateField>                  
                <ItemTemplate>                        
                   <asp:Button ID="ButtonReserve" style="background:#2ACC16; border-radius:8px;" runat="server" CommandName="Reserve" HeaderText="BOOK SEAT" Text="Reserve" OnClick="ButtonReserve_Click" />                
                </ItemTemplate>            
             </asp:TemplateField> 

       </Columns>                      
   </asp:GridView>
5
  • I don't see anywhere either your code or what have you tried. Commented Apr 21, 2016 at 6:26
  • please show what code you have written to connect to db and fetch Commented Apr 21, 2016 at 6:30
  • 3
    You must have to show your code and at least SQL Query you are using Commented Apr 21, 2016 at 6:30
  • I have added source code. Commented Apr 21, 2016 at 6:34
  • where do you bind ? , there is a call to rep_bind(); what does it do? Commented Apr 21, 2016 at 6:44

3 Answers 3

5

You are using like operator in SQL side. It won't compare as it is rather will search for a pattern. In your example, it will get all the records has 5 in it.

For more info visit Like operator

To work around your solution use = operator that compare exact input.

select * from HallSeat where (SeatNumber = +'+ @searchNumber +'+ ) and (SeatRow  like '%' + @searchRow + '%')
Sign up to request clarification or add additional context in comments.

Comments

1

Because you are searching with like '%5%' try to search with = '5'

1 Comment

Thanks. I deleted all % symbols and leave ' ' and it worked fine.
0

you must bind the datareader to the gridview, like in this sample do not executer datarader.Read() before you do the binding.

 using (SqlConnection con = new SqlConnection(strConnString))
{
    using (SqlCommand cmd = new SqlCommand())
    {
        cmd.CommandText = "select top 10 * from Customers";
        cmd.Connection = con;
        con.Open();
        GridView1.DataSource = cmd.ExecuteReader();
        GridView1.DataBind();
        con.Close();
    }
}

And as said by the others your stored procedure is not ok. should be like this

ALTER Procedure [dbo].[Search]
@searchNumber int,
@searchRow int

as
Begin   
    select * from HallSeat where SeatNumber = @searchNumber and SeatRow = @searchRow

End

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.