出错时必须声明标量变量 [英] getting error must declare scalar variable
本文介绍了出错时必须声明标量变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
这是我的代码.
here is my code.
<asp:Content ID="Content2" ContentPlaceHolderID="MainContent" runat="server">
<asp:ScriptManager ID="scm" runat="server"></asp:ScriptManager>
<table>
<tr>
<td>
<asp:DropDownList ID="ddldepartment" runat="server"
onselectedindexchanged="ddldepartment_SelectedIndexChanged" AutoPostBack="true"></asp:DropDownList>
</td>
</tr>
<tr>
<td>
<asp:GridView ID="gv" runat="server" DataKeyNames="empid"></asp:GridView>
</td>
</tr>
</table>
</asp:Content>
在aspx.cs
in aspx.cs
public partial class GetDetailsbydropdown : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
CustomersBusiness cb = new CustomersBusiness();
if (!IsPostBack)
{
ddldepartment.DataSource = cb.GetDepartment();
ddldepartment.DataTextField = "departmentid";
ddldepartment.DataValueField = "departmentname";
ddldepartment.DataBind();
}
}
protected void ddldepartment_SelectedIndexChanged(object sender, EventArgs e)
{
CustomersBusiness cb=new CustomersBusiness();
Customers cs = new Customers();
cs.departmentid = Convert.ToInt32(ddldepartment.SelectedItem.Text);
gv.DataSource = cb.GetEmployeebyDepartment(cs);
gv.DataBind();
}
}
in businesslogic.cs
public List<customers> GetEmployeebyDepartment(Customers cs)
{
List<customers> lst = new List<customers>();
SqlConnection con = new SqlConnection(constring);
con.Open();
string query = "select empid, empname, departmentid, joiningdate, locationid, salary, jobid, isworking where departmentid=@departmentid";
cmd = new SqlCommand(query, con);
dr = cmd.ExecuteReader();
if (dr.HasRows)
{
while (dr.Read())
{
Customers cst = new Customers();
cst.employeeid = (int)dr[0];
cst.empname = (string)dr[1];
cst.departmentid = (int)dr[2];
cst.joiningdate = (DateTime)dr[3];
cst.locationid = (int)dr[4];
cst.salary = (int)dr[5];
cst.jobid = (int)dr[6];
cst.isworking = (Boolean)dr[7];
lst.Add(cst);
}
}
return lst;
}
我遇到错误,必须声明标量变量
请帮助我
谢谢.
i am getting error must declare scalar variable
please help me
thank you.
推荐答案
您的SQL正在使用@departmentid,而SQL不知道那是什么.您需要执行
Your SQL is using @departmentid and SQL does not know what that is. You need to do
cmd.Parameters.AddWithValue("@departmentid", deptid);
将您的查询更改为
Change your Query to
string query = "select empid, empname, departmentid, joiningdate, locationid, salary, jobid, isworking where departmentid="+cs.departmentid;
这篇关于出错时必须声明标量变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文