2
int no = FormView1.PageIndex;

Query:-

  SqlCommand cmd =  new SqlCommand("select Answer from Questions where QuestionNo = @no", cn);
2
  • Use prepared statements. Commented Jul 9, 2013 at 16:09
  • So what is happening with your code. Is it failing, throwing an execption, unexpected results?? Commented Jul 9, 2013 at 16:12

5 Answers 5

5

You have to add a parameter:

int no = FormView1.PageIndex;
SqlCommand cmd = 
    new SqlCommand("select Answer from Questions where QuestionNo = @no", cn);

// Set the parameter up before executing the command
cmd.Parameters.Add("@no", SqlDbType.Int);
cmd.Parameters["@no"].Value = no;
Sign up to request clarification or add additional context in comments.

Comments

1

You need to add a SqlParameter to the SqlCommand:

int no = FormView1.PageIndex;
SqlCommand cmd = new SqlCommand("select Answer from Questions where QuestionNo = @no", cn);
cmd.Parameters.AddWithValue("@no", no);

Comments

0

use an array of System.Data.SqlClient.SqlParameter

SqlCommand cmd(...);
SqlParameter[] inQueryParameters = ...;
cmd.Parameters.AddRange(inQueryParameters);

Comments

0

Use SqlParameters. See the example below.

http://msdn.microsoft.com/en-us/library/system.data.sqlclient.sqlcommand.parameters.aspx

Comments

0

You can use this code:

SqlCommand ageCom = new SqlCommand("select age_phase from PatintInfo where patient_ID=@ptn_id", con);
ageCom.Parameters.Add("@ptn_id",SqlDbType.Int).Value=Convert.ToInt32(TextBox1.Text);

It had worked in my program correctly.

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.