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 />
}
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 />
it gives me the error AMBIGOUS COLOMN NAME 'name'.Why it is not working with where clause?any help would be greatly appriciated