日期:2014-05-20  浏览次数:20520 次

这个函数有什么错误吗?
//根据单位名称计算其ID
private   string   GetOrganID(string   strOrg)
{
string   id= "20 ";
SqlConnection   Conn=new   SqlConnection(ConnStr);
string   strSql= "select   id   from   clientele   where   organ= "+strOrg.Trim();
SqlCommand   cmd=new   SqlCommand(strSql,Conn);
Conn.Open();
try
{
SqlDataReader   dr=cmd.ExecuteReader();
if(dr.Read())
{
id=dr[ "id "].ToString().Trim();
}
dr.Close();
}
catch(SqlException   Ex)
{
Response.Write(Ex.ToString());
}
finally
{
Conn.Close();
}
return   id;
}

private   void   Button1_Click(object   sender,   System.EventArgs   e)
{
string   strOrg=DropDownList4.SelectedItem.Text.ToString().Trim();
Response.Write( " <script> alert( 'ID= '+ ' "+   GetOrganID(strOrg)   + " '); </script> ");
}

------解决方案--------------------
string strSql= "select id from clientele where organ= ' "+strOrg.Trim()+ " ' ";
如果organ是nvarchar类型的话

------解决方案--------------------
string strSql= "select id from clientele where organ= "+strOrg.Trim(); ===> string strSql= "select id from clientele where organ= ' "+strOrg.Trim() + " ' ";