protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection("Data Source=HKGROX-PC\\SQLEXPRESS;Initial Catalog=example;Integrated Security=True");
con.Open();
string value = DropDownList1.SelectedValue.ToString();
switch (value)
{
case "BJP":
string s = "select BJP from noofvotes where year= "+ DropDownList2.Text+" and const=" + DropDownList3.SelectedValue + "";
SqlCommand cmd = new SqlCommand(s,con);
DataSet ds = new DataSet();
SqlDataAdapter sda = new SqlDataAdapter(cmd);
sda.Fill(ds);
if (ds.Tables[0].Rows.Count > 0)
{
DataRow dr = ds.Tables[0].Rows[0];
TextBox1.Text = dr["BJP"].ToString();
}
break;
}
con.Close();
}
這裏我得到了無效的列名'Rohini'。我有一個表格,將這些值傳遞給列表框。請檢查我的查詢中的錯誤,我收到「無效的列名稱」錯誤。
年是一個保留關鍵字。什麼是你的表的真實列名? – Steve
你在哪裏看到這個錯誤?這段代碼中沒有提到'Rohini'? – christiandev
我想'DropDownList3.SelectedValue'包含值「Rohini」,它不被'''包圍,所以它被解釋爲一列('where const = Rohini')。按照@Steve的建議使用參數。 – CodeCaster