2012-05-17 21 views
4

我使用的SqlDataSource顯示根據登錄的用戶記錄,必須聲明標量變量「@ LoggedInUser」。錯誤嘗試參數添加到SqlDataSource的

​​

,並在頁面加載我加入參數如下

protected void Page_Load(object sender, EventArgs e) 
     { 
      string user = HttpContext.Current.User.Identity.Name; 
      ModifyCustomerDataSource.SelectParameters.Clear(); 
      ModifyCustomerDataSource.SelectParameters.Add("@LoggedInUser", user.Trim()); 
     } 

時但它給我錯誤

Must declare the scalar variable "@LoggedInUser". 
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. 

Exception Details: System.Data.SqlClient.SqlException: Must declare the scalar variable "@LoggedInUser". 

有人可以指出我哪裏出錯了。 由於

+0

這是使用sqldatasource的最佳實踐嗎?只是好奇。我正在解決不使用sqldatasource。如果你想要示例代碼,請讓我知道.. – AnandMohanAwasthi

+0

@AnandMohanAwasthi,我綁定這個數據到我的jqGrid,所以這對我來說更簡單了,好吧沒有太多的想法是最好的做法,你可以問富好吧關於它。示例代碼將幫助我。謝謝 – freebird

回答

10

添加參數例如,當你不需要的@

ModifyCustomerDataSource.SelectParameters.Add("LoggedInUser", user.Trim()); 

查詢中的@符號表示以下文本是參數的名稱,但@不被視爲參數名稱的一部分。

+0

爲我工作了很多。 – freebird

+1

因爲今晚有太多時間和太多不愉快的話。 –

1
<asp:SqlDataSource ID="ModifyCustomerDataSource" SelectCommand="SELECT cApplicationNo,cFirstName,cMiddleName,cLastName,nTelNo,nMobileNo,cPanGirNo from Data_Customer_Log where [email protected]" ConnectionString="<%$ ConnectionStrings:CwizDataConnectionString %>" runat="server"> 

     <SelectParameters> 

      <asp:Parameter Name="LoggedInUser" /> 

     </SelectParameters> 

</asp:SqlDataSource> 

請確認您有在限定的SqlDataSource如SelectParameter樣品如上所述。

您可以設置參數的值在兩個方面:

ModifyCustomerDataSource.SelectParameters["LoggedInUser"].DefaultValue = "some value"; 

or 

ModifyCustomerDataSource.SelectParameters.Add("@LoggedInUser", user.Trim()); //@is must here with the parameter name 
+0

問題已解決,豐富的答案幫助。感謝您的答案。 – freebird

+0

ModifyCustomerDataSource.SelectParameters.Add(「@ LoggedInUser」,user.Trim()); ,沒有工作,刪除@爲我工作 – freebird

+0

它的工作原因是你沒有提到Aspx中的「LoggedInUser」的標籤根據我的示例。 –