如何将dropdownlist值传递到asp.net中的sql命令

如何将dropdownlist值传递到asp.net中的sql命令

问题描述:

Hello All,



我创建了一个Dropdown菜单&手动传递一些ListItems,还创建了按钮和标签,以便在单击按钮时显示数据库中的结果数据。



现在,如何在SQL中传递DropDown Selected值C#中的命令?



错误:

Hello All,

I have created a Dropdown menu & passed some ListItems manually, also created button and label to display resultant data from the database whenever the button is clicked.

Now, how to pass DropDown Selected value in the SQL Command in C#?

Error:

An exception of type 'System.Data.SqlClient.SqlException' occurred in System.Data.dll but was not handled in user code

Additional information: Incorrect syntax near '(Telangana)'.





注意:Telangana是从DropDown菜单传递的状态。



我尝试过:



//以下是按钮事件代码。



protected void Button1_Click(object sender,EventArgs e)

{

SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings [DefaultConnection]。ConnectionString);





string result =SELECT(SELECT Count(case when [IT-ITES1] ='已批准'然后1结束)FROM CAPACITY WHERE State ='(+ DropDownList1.SelectedValue +)';

string result2 =select PART(Candidate)from PARTNER ;



SqlCommand showresult = new SqlCommand(result,con);

SqlCommand showresult2 = new SqlCommand(result2,con);

con.Open();

Label2.Text = showresult.ExecuteScalar()。ToString();

Label3.Text = showresult2.ExecuteScalar( ).ToString();

con.Close();

}



//错误:不正确关键字'from'附近的语法。



Note: "Telangana" is a state passed from DropDown Menu.

What I have tried:

//Below is the button event code.

protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["DefaultConnection"].ConnectionString);


string result = "SELECT (SELECT Count(case when [IT-ITES1] = 'Approved' then 1 end) FROM CAPACITY WHERE State = '(" + DropDownList1.SelectedValue + ")'";
string result2 = "select sum(Candidate) from PARTNER";

SqlCommand showresult = new SqlCommand(result, con);
SqlCommand showresult2 = new SqlCommand(result2, con);
con.Open();
Label2.Text = showresult.ExecuteScalar().ToString();
Label3.Text = showresult2.ExecuteScalar().ToString();
con.Close();
}

//Error: Incorrect syntax near the keyword 'from'.

请在您的内联查询中加上引号。



Be低价是要改变的行



不正确:



Please put quote in your inline query.

Below are the lines to change

Incorrect:

string result = "SELECT (SELECT Count(case when [IT-ITES1] = 'Approved' then 1 end) FROM CAPACITY WHERE State = '(" + DropDownList1.SelectedValue + ")'";





正确:





Correct:

string result = "SELECT (SELECT Count(case when [IT-ITES1] = 'Approved' then 1 end) FROM CAPACITY WHERE State = '('" + DropDownList1.SelectedValue + "')'";





谢谢,

Parveen Siwach



Thanks,
Parveen Siwach


string result = "SELECT (SELECT Count(case when [IT-ITES1] = 'Approved' then 1 end) FROM CAPACITY WHERE State = '" + DropDownList1.SelectedValue + "' ";