Click here to Skip to main content
15,887,477 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
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
Posted

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

C#
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);
 
Share this answer
 
Comments
kishore Rajendran 26-Feb-13 3:48am    
(y)
use
where emp_details.name=

in place of
where name=
 
Share this answer
 
specify on what table the column name came from

like this

emp_details.name
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900