在asp web表单中选择WHERE

时间:2014-05-06 09:36:53

标签: asp.net webforms

我目前在ASP Web Forms上遇到很多问题我试图从数据库中获取数据并根据用户在表单的文本框中输入的内容对其进行过滤。

到目前为止,我已经得到了这个,如果我删除最后一位有关创建的查询的数据,它会从数据库中选择数据但是一旦我开始尝试按照它创建的过滤器说必须声明标量变量@ 1。我此刻只是碰到了一堵砖墙

@{
    var db1 = Database.Open("<database name>");
    var selectCMD = "SELECT CompanyName, FirstName, LastName, Description, LeadSourceCode, CreatedOn FROM LeadBase INNER JOIN LeadExtensionBase ON LeadBase.LeadId = LeadExtensionBase.LeadId WHERE (CreatedOn BETWEEN CAST(DATEADD(dd, - 140, GETDATE()) AS DATE) AND CAST(DATEADD(dd, -7, GETDATE()) AS DATE)) AND (LeadExtensionBase.new_GenActivity = 0)";

if(!Request.QueryString["searchId"].IsEmpty()) 
{ 
    searchTerm1 = Request.QueryString["searchId"];
    selectCMD = "SELECT CompanyName, FirstName, LastName, Description, LeadSourceCode, CreatedOn FROM LeadBase INNER JOIN LeadExtensionBase ON LeadBase.LeadId = LeadExtensionBase.LeadId WHERE (CreatedOn BETWEEN CAST(DATEADD(dd, - 140, GETDATE()) AS DATE) AND CAST(DATEADD(dd, -7, GETDATE()) AS DATE)) AND (LeadExtensionBase.new_GenActivity = 0) AND (LeadBase.CreatedBy = @1)";
}
var selectedDBdata = db1.Query(selectCMD, searchTerm1);

}

    <form method="get">
        <table>
            <tr>
                <td>Enter Employee ID</td>
                <td><input class="id" name="searchId" type="text"/></td>
            </tr>
            <tr>
                <td>Enter Date From</td>
                <td><input name="searchDF" style="cursor: text" value=""/></td>
            </tr>
            <tr>
                <td>Enter Date To</td>
                <td><input name="searchDT" style="cursor: text" value=""/></td>
            </tr>
            <tr>
                <td><input type="submit" value="Filter"></td>
            </tr>
        </table>

    </form>

            @if (!Request.QueryString["searchId"].IsEmpty() ) {
            <table class="data-table">
                <tr>
                    <th>Company Name</th>
                    <th>First Name</th>
                    <th>Last Name</th>
                    <th>Description</th>
                    <th>Lead Source Code</th>
                </tr>
                @foreach(var row in db1.Query(selectCMD)){
                    <tr>
                        <td><input class="tdtext" readonly="readonly" type="text" value="@row.CompanyName"></td>
                        <td><input class="tdtext" readonly="readonly" type="text" value="@row.FirstName"></td>
                        <td><input class="tdtext" readonly="readonly" type="text" value="@row.LastName"></td>
                        <td><textarea class="tdtextbox" readonly="readonly">@row.Description</textarea></td>
                        <td><input readonly="readonly" class="tdtext" type="text" value="@row.LeadSourceCode"></td>
                    </tr>
                }
            </table>
                }

2 个答案:

答案 0 :(得分:1)

尝试将selectCMD中的@ 1更改为@ 0

传递给db1.Query(selectCMD,searchTerm1)的第一个对象映射到@ 0而不是@ 1

答案 1 :(得分:0)

尝试在第二个选择查询中删除@ 1,因为您尚未向您的任何位置声明@ 1。如果您仍然遇到任何问题,请告诉我。

像这样改变,

selectCMD = "SELECT CompanyName, FirstName, LastName, Description, LeadSourceCode, CreatedOn FROM LeadBase INNER JOIN LeadExtensionBase ON LeadBase.LeadId = LeadExtensionBase.LeadId WHERE (CreatedOn BETWEEN CAST(DATEADD(dd, - 140, GETDATE()) AS DATE) AND CAST(DATEADD(dd, -7, GETDATE()) AS DATE)) AND (LeadExtensionBase.new_GenActivity = 0) AND (LeadBase.CreatedBy = @1)
selectCMD.Parameters.AddwithValue("@1",'"+TextBox1.Text+"'); // Your textbox values