Click here to Skip to main content
15,900,511 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
taking the value from dropdownlist and showing in gridview.with this code when i press button nothing happen and no error come.please help me out with that
C#
string a = DropDownList1.SelectedValue.ToString();
SqlConnection con = new SqlConnection(connstring);
string query="SELECT * FROM ['ISB VAS Nodes$'] WHERE Model='"+DropDownList1.Text+"'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();
da.Fill(dt);
GridView1.DataSource = dt;
GridView1.DataBind();
Posted
Updated 21-Apr-13 20:10pm
v2
Comments
PrashantSonewane 22-Apr-13 0:56am    
First of all correct the code at string query="SELECT * FROM ['ISB VAS Nodes$'] WHERE Model='"+DropDownList1.Text+"'"; as string query="SELECT * FROM ['ISB VAS Nodes$'] WHERE Model='"+ a +"'"; a is variable where you collect the dropdown value. Let us know if that is also not working.
fak_farrukh 22-Apr-13 1:03am    
i have change but same issue
Mojtaba Eng 22-Apr-13 1:05am    
hi:
check filled DataTable Rows Count : MessageBox.Show(dt.Rows.Count.ToString());
if it nonzero , go for datagrid if have problem yet.
fak_farrukh 22-Apr-13 1:09am    
can't understand
Chinmaya C 22-Apr-13 1:07am    
Where you have written the above code. Can you please post the eventsource of the mentioned code.

1 solution

Hi,

Only Possible way is check Data table row count
string a = DropDownList1.SelectedValue.ToString();
SqlConnection con = new SqlConnection(connstring);
string query="SELECT * FROM ['ISB VAS Nodes$'] WHERE Model='"+DropDownList1.Text+"'";
SqlDataAdapter da = new SqlDataAdapter(query, con);
DataTable dt = new DataTable();

da.Fill(dt);

if(dt.rows.Count>0)
{
 GridView1.DataSource = dt;
 GridView1.DataBind();
}
else
{
 lblResult.Text = "No Records Found";
}

And You may Add below code to gridview design for display Empty message
<emptydatatemplate>
No Records Found
</emptydatatemplate>


When execute this, If you Got this message - that Means DB Table does not Contain Records, that match to your query

Thanks

Siva Rm K
 
Share this answer
 
Comments
fak_farrukh 22-Apr-13 6:38am    
i'm getting the message "No Records Found".
tell me what is the problem
rmksiva 22-Apr-13 6:50am    
Hi, Problem In query not Code. Print query value in any label
lblResult.Text = query; Check if any record found related to your query !

change the line
string query="SELECT * FROM ['ISB VAS Nodes$'] WHERE Model='"+DropDownList1.SelectedValue+"'";

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