哪个条款没有选择价值,为什么?

时间:2014-03-12 18:41:45

标签: c# asp.net class webforms ado.net

我在注册的基础上过滤记录不喜欢它应该只选择'1002'的记录,但它没有,它选择所有记录,为什么?

string connStr = ConfigurationManager.ConnectionStrings["ETT"].ConnectionString;
    protected void Page_Load(object sender, EventArgs e)
    {
        SqlConnection sqlCon = new SqlConnection(connStr);
        SqlCommand sqlCom = new SqlCommand("Vehicleledger", sqlCon);
        sqlCom.Parameters.Add("@RegistrationNo", SqlDbType.VarChar);
        sqlCom.Parameters["@RegistrationNo"].Value = "1002";
        SqlDataAdapter sqlDA = new SqlDataAdapter(sqlCom);
        DataSet ds = new DataSet();
        sqlDA.Fill(ds);

        try
        {
            sqlCon.Open();
            gvLiveTransaction.DataSource = ds;
            gvLiveTransaction.DataBind();
            sqlCom.ExecuteNonQuery();
        }
        catch (Exception exc)
        {
            Response.Write("Error:" + exc.Message);
        }
        finally
        {
            sqlCon.Close();
        }
    }
}

源代码:

<form id="form1" runat="server">
        <div>
         <asp:GridView ID="gvLiveTransaction" runat="server" DataKeyNames="RNo" 
             AutoGeneratedColumns="false" CellPadding="4" ForeColor="#333333" GridLines="None">
             <AlternatingRowStyle BackColor="White" />
             <EditRowStyle BackColor="#2461BF" />
             <FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
             <PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
             <RowStyle BackColor="#EFF3FB" />
             <SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
             <SortedAscendingCellStyle BackColor="#F5F7FB" />
             <SortedAscendingHeaderStyle BackColor="#6D95E1" />
             <SortedDescendingCellStyle BackColor="#E9EBEF" />
             <SortedDescendingHeaderStyle BackColor="#4870BE" />
         </asp:GridView>
        </div>
        </form>

我正在调用这个存储过程,当我传递相同的值而不是在asp.net中时,它在sql server中运行正常,为什么?

ALTER procedure [dbo].[Vehicleledger]
@RegistrationNo varchar(20)
as

begin

SELECT  dbo.Vehicle.RNo AS RegistrationNo, dbo.TransactionTable.DateOfTransaction, dbo.TransactionTable.RNo, dbo.EngineCapacity.EngineCapcity, 
               dbo.MainCategories.Name, dbo.Categories.Description, dbo.TransactionTable.NoOfMonth, dbo.TransactionTable.Amount
FROM  dbo.Categories INNER JOIN
               dbo.MainCategories ON dbo.Categories.MainCategory_ID = dbo.MainCategories.CatID INNER JOIN
               dbo.TransactionTable ON dbo.Categories.ID = dbo.TransactionTable.CategoryID INNER JOIN
               dbo.EngineCapacity ON dbo.TransactionTable.EngineCapacityID = dbo.EngineCapacity.ID INNER JOIN
               dbo.Vehicle ON dbo.TransactionTable.RNo = dbo.Vehicle.RNo
where dbo.Vehicle.RNo=@RegistrationNo
end

1 个答案:

答案 0 :(得分:0)

我通常会添加这样的参数。你可以在你的代码中尝试这个吗?

...
SqlParameter parameter = new SqlParameter("@RegistrationNo", SqlDbType.VarChar);
parameter.IsNullable = false;    
parameter.Size = 20;
parameter.Value = "1002";
sqlCom.Parameters.Add(parameter);
...