user2565309
user2565309

Reputation: 21

How to pass integer variable in sql query

int no = FormView1.PageIndex;

Query:-

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

Upvotes: 2

Views: 3879

Answers (5)

wittyse
wittyse

Reputation: 29

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.

Upvotes: 0

asafrob
asafrob

Reputation: 1858

use an array of System.Data.SqlClient.SqlParameter

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

Upvotes: 0

D Stanley
D Stanley

Reputation: 152596

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

Upvotes: 1

Reed Copsey
Reed Copsey

Reputation: 564631

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;

Upvotes: 5

Related Questions