0

I have following code that updates the value of query parameters of a SQL Data Source after going through some conditions.

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();

rid is already set in query string as ?rid=a

2 Answers 2

0

Shouldn't the query string be ?replyid=a instead of ?rid=a?.

It seems that Request["replyid"] returns null, and then ToString() throws the exception.

Sign up to request clarification or add additional context in comments.

1 Comment

If this is indeed the problem, the question should be closed, as simple typos are too localized to help anyone else.
0

You'll want to use the Request.QueryString collection to get your rid parameter out:

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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.