如何将dropdownlist值传递到asp.net中的sql命令 [英] How do i pass dropdownlist value into sql command in asp.net

查看:73
本文介绍了如何将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 + "' ";


这篇关于如何将dropdownlist值传递到asp.net中的sql命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆