在dropdownlist_SelectedIndexChanged上绑定gridview [英] Binding gridview on dropdownlist_SelectedIndexChanged
问题描述
大家好,
在我的应用程序中,我试图将来自两个表(emp_details,leave_bal)的数据绑定到 dropdownlist4.SelectedIndexChanged 中的gridview b> event.the页面的代码如下:
Hello everyone,
In my application i am trying to bind data from two tables(emp_details,leave_bal) to gridview on dropdownlist4.SelectedIndexChanged event.the code of the page is as follows:
<asp:DropDownList ID="DropDownList4" runat="server" AutoPostBack="true" DataTextField="name"<br />
DataValueField="name" OnSelectedIndexChanged="DropDownList4_SelectedIndexChanged"><br />
</asp:DropDownList><br />
<br />
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="false" CellPadding="10"<br />
BorderStyle="Solid" BorderColor="Black" BorderWidth="1px" GridLines="None" Font-Names="Arial" CssClass="grid"><br />
<br />
<RowStyle BackColor="#70B8FF" ForeColor="#333333" HorizontalAlign="Center" BorderStyle="Solid"<br />
BorderColor="Black" BorderWidth="1px" /><br />
<br />
<HeaderStyle BackColor="#197ABA" Font-Bold="True" ForeColor="Black" /><br />
<br />
<Columns><br />
<br />
<asp:BoundField DataField="uname" HeaderText="Username" SortExpression="uname" /><br />
<asp:BoundField DataField="dept" HeaderText="Department" SortExpression="dept" /><br />
<asp:BoundField DataField="jdate" HeaderText="Joining Date" SortExpression="jdate" /><br />
<asp:BoundField DataField="cl" HeaderText="CL" SortExpression="cl" /><br />
<asp:BoundField DataField="sl" HeaderText="SL" SortExpression="sl" /><br />
<asp:BoundField DataField="hl" HeaderText="HL" SortExpression="hl" /><br />
<asp:BoundField DataField="ml" HeaderText="ML" SortExpression="ml" /><br />
<asp:BoundField DataField="pl" HeaderText="PL" SortExpression="pl" /><br />
<br />
</Columns><br />
<br />
</asp:GridView>
代码为如下:
Code for this is as follows:
<br />
protected void DropDownList4_SelectedIndexChanged(object sender, EventArgs e)<br />
{<br />
SqlConnection conn = new SqlConnection();<br />
conn.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings["leave"]<br />
.ConnectionString;<br />
<br />
conn.Open();<br />
<br />
SqlCommand cmd = new SqlCommand("select emp_details.uname,emp_details.dept,emp_details.jdate,leave_bal.cl,<br />
leave_bal.sl,leave_bal.hl,leave_bal.ml,leave_bal.pl from emp_details INNER JOIN leave_bal ON emp_details.code=leave_bal.code", conn);<br />
<br />
SqlDataAdapter da = new SqlDataAdapter(cmd);<br />
<br />
DataSet ds = new DataSet();<br />
<br />
da.Fill(ds);<br />
<br />
GridView1.DataSource = ds;<br />
<br />
GridView1.DataBind();<br />
}
数据库字段是
emp_details:name,code,uname,dept,jdate
leave_bal:name,cl,sl,hl,ml,pl,code
它可以正常使用上面的查询,但当我改变它如下(添加where子句)
database fields are
emp_details: name,code,uname,dept,jdate
leave_bal:name,cl,sl,hl,ml,pl,code
it works fine with the above query but when i change it like below(adding where clause)
SqlCommand cmd = new SqlCommand("select emp_details.uname,emp_details.dept,emp_details.jdate,leave_bal.cl,<br />
leave_bal.sl,leave_bal.hl,leave_bal.ml,leave_bal.pl from emp_details INNER JOIN leave_bal ON emp_details.code=leave_bal.code where name=''"+DropDownList4.SelectedValue+"''", conn);<br />
它给了我错误AMBIGOUS COLOMN NAME''name''。为什么它不使用where子句?任何帮助都会非常适合
it gives me the error AMBIGOUS COLOMN NAME ''name''.Why it is not working with where clause?any help would be greatly appriciated
推荐答案
所以这意味着,错误本身就是说,两个表都有与name相同名称的列,但在Where条件,你没有提到表名,如emp_details.name
请参阅以下更新代码
so it means, the error itself says, the both tables have column with same name as "name", but in "Where" condition, u haven''t mentioned table name like, "emp_details.name"
see the following updated code
SqlCommand cmd = new SqlCommand("select emp_details.uname,emp_details.dept,emp_details.jdate,leave_bal.cl,
leave_bal.sl,leave_bal.hl,leave_bal.ml,leave_bal.pl from emp_details INNER JOIN leave_bal ON emp_details.code=leave_bal.code where emp_details.name='"+DropDownList4.SelectedValue+"'", conn);
使用
其中emp_details.name =
代替
其中name =
指定co在哪个表上lumn名字来自
喜欢这个
emp_details.name
specify on what table the column name came from
like this
emp_details.name
这篇关于在dropdownlist_SelectedIndexChanged上绑定gridview的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!