此代码在下面标记的行上引发了错误,有人可以指出正确的方向为什么引发错误吗?
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["LocalSqlServer"].ConnectionString);
SqlDataAdapter dadapter;
DataSet dset;
string sql = "SELECT * from SocoetyMaintan";
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
dadapter = new SqlDataAdapter(sql, con);
dset = new DataSet();
dadapter.Fill(dset);
DropDownList1.DataSource = dset.Tables[0];
DropDownList1.DataTextField = "FullName";
DropDownList1.DataValueField = "Id";
DropDownList1.DataBind();
GridViewBind();
}
}
protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
GridViewBind();
}
public void GridViewBind()
{
dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id=" + DropDownList1.SelectedValue + "", con);
dset = new DataSet(); //Throwing Error Here
dadapter.Fill(dset);
GridView1.DataSource = dset.Tables[0];
GridView1.DataBind();
答案 0 :(得分:0)
在这里,您忘记了将DropDownList1.SelectedValue
转换为字符串或int数据库中的任何内容。因此,请按照以下步骤进行操作。
例如,
dadapter = new SqlDataAdapter("select * from SocoetyMaintan where Id='" + DropDownList1.SelectedValue.ToString() + "';", con);
答案 1 :(得分:0)
它认为您在e.Message.Data
中缺少引号。
Id=
顺便说一句。您绝对不应允许用户直接输入查询。查找“ SQL注入”。
答案 2 :(得分:0)
您的书写方式可能会出错。尝试以下
方法1:
LocalSettings
方法2:(首选)
SqlDataAdapter dadapter = new SqlDataAdapter(string.Format("SELECT * FROM SocoetyMaintan WHERE Id = '{0}'",DropDownList1.SelectedValue), con);
答案 3 :(得分:0)
1)只需尝试如下更改查询
@objc func checkForReachability(notification:NSNotification)
{
let reachability = notification.object as! Reachability
switch reachability.connection {
case .wifi:
print("Reachable via WiFi")
case .cellular:
print("Reachable via Cellular")
case .none:
print("Network not reachable")
}
}
注意:更好地在查询中使用准备好的语句。
dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id='" + DropDownList1.SelectedValue + "'", con);
2)尝试设置dadapter = new SqlDataAdapter("SELECT * from SocoetyMaintan where Id=@Id", con);
dadapter.SelectCommand.Parameters.AddWithValue("@Id", DropDownList1.SelectedValue);
=> asp:GridView
和DataSourceID="GridDataSource"
=> asp:SqlDataSource