Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在C中通过编程方式按值选择下拉列表项并将其传递给integer?_C#_Asp.net_Sql - Fatal编程技术网

C# 如何在C中通过编程方式按值选择下拉列表项并将其传递给integer?

C# 如何在C中通过编程方式按值选择下拉列表项并将其传递给integer?,c#,asp.net,sql,C#,Asp.net,Sql,然后调用dropdownlist event SelectedIndexChanged中的函数 public DataTable selectcat(int catid) { SqlConnection con = new SqlConnection(DBHelper.connection); SqlCommand com = new SqlCommand(); com.Connection = con; con.Open(); com.Comman

然后调用dropdownlist event SelectedIndexChanged中的函数

   public DataTable selectcat(int catid)
{
    SqlConnection con = new SqlConnection(DBHelper.connection);
    SqlCommand com = new SqlCommand();
    com.Connection = con;
    con.Open();
    com.CommandType = CommandType.StoredProcedure;
    com.CommandText = "selectcatalog";
    com.Parameters.AddWithValue("@catid", catid);
    SqlDataReader dr = com.ExecuteReader();
    DataTable dt = new DataTable();  
        dt.Load(dr);
        con.Close();
        return dt;
}
但它在dt=obj.selectcatselectddl.SelectedValue处给了我无效的参数错误;
PS:dropdownlist获取其显示和数据库中的值

只需将其转换为int:

 protected void catselectddl_SelectedIndexChanged(object sender, EventArgs e)
{
   DataTable dt;

    DAcatagory obj = new DAcatagory();
    dt = obj.selectcat(catselectddl.SelectedValue);
    DataRow dr = dt.Rows[0];
    catnametxt.Text = dr["catname"].ToString();
    catdestxt.Text = dr["catdescription"].ToString();
}
试着替换

dt = obj.selectcat(Convert.ToInt32(catselectddl.SelectedValue));

在debuggin模式下,还要检查ddl发送到db的实际值


此外,如果db希望使用int作为值,则将其转换为int32,就像其他答案一样。

catselectddl.SelectedValue是一个字符串如果我没有从该类中获取对象,我将无法使用该函数抱歉,很抱歉,我只是注意到了它,我没有进行dropdownlist回发
  dt = obj.selectcat(catselectddl.SelectedValue);
  dt = selectcat(catselectdd1.SelectedValue);