Click here to Skip to main content
15,892,768 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have use inner join to get my values from database, I want it to appear in my dropdownlist, but why my dropdownlist do not have the values from database , here is my code.

C#
if (!IsPostBack) //aspx.cs
{
ListItem> allrID = new List<item>();
allID = diBLL.rID();
ddl_ID.DataSource = allID;
ddl_ID.DataBind();
ddl_ID.Items.Insert(0, new ListItem("--Select--", "0"));
}



//aspxID :<asp:DropDownList ID="ddl_ID" runat="server" DataTextField="ID" DataValueField="ID"



C#
//sql

public List<item> getrID()
{
List<item> IDAll = new List<tem>();

int ID;

string queryStr = "Select Item.ID FROM Item JOIN Food on Item.ID= FoodID ";

SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(queryStr, conn);

conn.Open();
SqlDataReader dr = cmd.ExecuteReader();

while (dr.Read())
{
string id = dr["Food.ID"].ToString();
ID = int.Parse(id);
IDAll.Add(new Item (ID));
}

conn.Close();
dr.Close();
dr.Dispose();

return IDAll;

}
Posted
Updated 7-Feb-14 3:32am
v5
Comments
ZurdoDev 7-Feb-14 9:14am    
Just trace it and find out. Run that SQL in SQL Management Studio and see what you get.
Member 10574155 7-Feb-14 9:24am    
The query is correct, but when I run the data is empty.
V. 7-Feb-14 9:24am    
Shouldn't it be INNER JOIN instead of JOIN ?
Member 10574155 7-Feb-14 9:32am    
I change to inner join still can't

1 solution

Try this
C#
public List<item> getrID()
{
List<item> IDAll = new List<item>();
 
int ID;
 
string queryStr = "Select Item.ID FROM Item JOIN Food on Item.ID= Food.ID ";
 
SqlConnection conn = new SqlConnection(_connStr);
SqlCommand cmd = new SqlCommand(queryStr, conn);
 
conn.Open();
SqlDataReader dr = cmd.ExecuteReader();
 
while (dr.Read())
{
string id = dr["ID"].ToString();
ID = int.Parse(id);
IDAll.Add(new Item (ID));
}
 
conn.Close();
dr.Close();
dr.Dispose();
 
return IDAll;
 
}</item></item></item>
 
Share this answer
 
Comments
Karthik_Mahalingam 7-Feb-14 10:34am    
can u share the screen ?
if possible in team viewer ?
Karthik_Mahalingam 7-Feb-14 10:40am    
ok
Karthik_Mahalingam 7-Feb-14 10:46am    
ok

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

  Print Answers RSS
Top Experts
Last 24hrsThis month


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