Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# Can';无法从ASP.NET中的SQL数据库中获取准确的值_C#_Asp.net - Fatal编程技术网

C# Can';无法从ASP.NET中的SQL数据库中获取准确的值

C# Can';无法从ASP.NET中的SQL数据库中获取准确的值,c#,asp.net,C#,Asp.net,我正在创建ASP.NET,C#应用程序。在SQL server的gridview中显示键入值结果时,其中有两个文本框。问题是,当我输入数字5时,我得到了15的值。有人能给个提示吗 如何准确地从数据库中获取文本框中键入的值 源代码: SQL过程 Create table Hall ( HallID int, Name nvarchar(50), TicketLimit int ) GO Create table HallGroup ( HallID int,

我正在创建ASP.NET,C#应用程序。在SQL server的gridview中显示键入值结果时,其中有两个文本框。问题是,当我输入数字5时,我得到了15的值。有人能给个提示吗

如何准确地从数据库中获取文本框中键入的值

源代码:

SQL过程

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
代码隐藏

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>




.按钮{ 背景:白色; 颜色:黑色; 边框:实心3px#6496c8; 字体系列:草书; 字号:18px; } .按钮:悬停, .按钮.悬停 { 边框颜色:#346392; 背景:7ABCDC; 颜色:白色; } .按钮:激活{ 边框颜色:#27496d; 颜色:27496d; }
因为您正在使用类似“%5%”的
进行搜索
尝试使用
=“5”
在SQL端使用
类似的
运算符进行搜索。它不会比较,因为它会搜索模式。在您的示例中,它将获取其中包含
5
的所有记录

欲了解更多信息,请访问

要解决此问题,请使用比较精确输入的
=
运算符

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

必须将datareader绑定到gridview,如本示例所示 在进行绑定之前,不要执行datarader.Read()

 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();
    }
}
正如其他人所说,您的存储过程不正常。 应该是这样的

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

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

End

我在任何地方都看不到您的代码或您尝试过的内容。请显示您为连接到db和FETCH而编写的代码。您必须显示您的代码和至少您正在使用的SQL查询。我已添加源代码。您绑定到哪里,有一个对rep_bind()的调用;它有什么作用?谢谢。我删除了所有%符号并保留了“”,效果很好。