简体   繁体   English

更新SQL数据源中的查询参数

[英]updating query parameters in SQL Data Source

I have following code that updates the value of query parameters of a SQL Data Source after going through some conditions. 我有以下代码,在经过某些条件后可以更新SQL数据源的查询参数的值。

SqlDataSource sdm = new SqlDataSource("<our connection string>", "SELECT  [Message Id] ,[To] ,[From] ,[Reply To] ,[Subject] ,[Message] ,[Date added] FROM [database].[dbo].[User Messages] where ([to]=@sid or [from]=@sid) and ([to]=@qid or [from]=@qid)  and [reply to]=@rid");

string user = Session["USERID"].ToString();
string to = Request["to"].ToString();
string from = Request["from"].ToString();
sdm.UpdateParameters["rid"].DefaultValue = Request["replyid"].ToString();
sdm.UpdateParameters["sid"].DefaultValue = user;
if (user == to)
  sdm.UpdateParameters["qid"].DefaultValue=from;
else
  sdm.UpdateParameters["qid"].DefaultValue= to;

sdm.Update();
sdm.DataBind();
Repeater1.DataSource = sdm;
Repeater1.DataBind();

Its showing NullReferenceException at sdm.UpdateParameters["rid"].DefaultValue = Request["replyid"].ToString(); 它在sdm.UpdateParameters["rid"].DefaultValue = Request["replyid"].ToString();显示NullReferenceException sdm.UpdateParameters["rid"].DefaultValue = Request["replyid"].ToString();

rid is already set in query string as ?rid=a rid已经在查询字符串中设置为?rid=a

You'll want to use the Request.QueryString collection to get your rid parameter out: 您将要使用Request.QueryString集合来获取rid参数:

sdm.UpdateParameters["rid"].DefaultValue = Request.QueryString["rid"];

Using an indexer with Request will get you values out of the QueryString , Form , Cookies or ServerVariables collections, using Request.QueryString directly makes your intentions that little bit clearer. 将索引器与Request将使您从QueryStringFormCookiesServerVariables集合中获取值,而使用Request.QueryString可以使您的意图更加清晰。

Shouldn't the query string be ?replyid=a instead of ?rid=a? 查询字符串不应该是?replyid=a而不是?rid=a? .

It seems that Request["replyid"] returns null, and then ToString() throws the exception. 似乎Request["replyid"]返回null,然后ToString()引发异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM