Hello,
I have a database table which include
<pre lang="text">
Id name email category password address
1 jibson doc@admin.in birds 211301 Abc Villa
2 jin doc2@admin.in dog 211301 abc village
3 test doc3@admin.in dog 211301 accc
So i am trying to create a Ask a Question Page in Asp.net , so that my users can Ask questions to expert users from their dashboard.
To Achieve
Clients can choose the category via the DropDownList , For Example.
First the user Choose a category from the dropdownlist and in the second dropdownlist the user can see the name of doctors in the selected category.
SELECT Category --> Choose Name ----> Then Fill the Text Box with Question and Send
What i have done
I have created the above database and two dropdown list namely ddlcategory and ddldoctors.
And i tried the below code, but its not working;.
The Code i Tried
The problem is that, the values in the category field are showing same as in the order like in the database.,
CHOOSE CATEGORY
birds
dog
dog
But i only need it show only once ( Not duplicate)
What I have tried:
My askdoc.asp.cs
using System;
using System.Collections.Generic;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Configuration;
public partial class askdoc : System.Web.UI.Page
{
string em;
DbConnect db = new DbConnect();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
string query = "select Id, type from docreg";
BindDropDownList( ddlcategory, query, "type", "Id", "Select Category");
ddldoctors.Enabled = false;
ddldoctors.Items.Insert(0, new ListItem("Select Doctor", "0"));
}
}
protected void State_Changed(object sender, EventArgs e)
{
ddldoctors.Enabled = false;
ddldoctors.Items.Clear();
ddldoctors.Items.Insert(0, new ListItem("Select Doctor", "0"));
int stateId = int.Parse(ddlcategory.SelectedItem.Value);
if (stateId > 0)
{
string query = string.Format("select Id, name from docreg where Id = {0}", stateId);
BindDropDownList(ddldoctors, query, "name", "Id", "Select Doctor");
ddldoctors.Enabled = true;
}
}
private void BindDropDownList(DropDownList ddl, string query, string text, string value, string defaultText)
{
string conString = ConfigurationManager.ConnectionStrings["petlyConnectionString"].ConnectionString;
SqlCommand cmd = new SqlCommand(query);
using (SqlConnection con = new SqlConnection(conString))
{
using (SqlDataAdapter sda = new SqlDataAdapter())
{
cmd.Connection = con;
con.Open();
ddl.DataSource = cmd.ExecuteReader();
ddl.DataTextField = text;
ddl.DataValueField = value;
ddl.DataBind();
con.Close();
}
}
ddl.Items.Insert(0, new ListItem(defaultText, "0"));
}
}
A code snipet of askdoc.asp
<h3>Have a Question ? Then Ask to Our Certified Expert's </h3>
<div class="modal-body">
<table width="100%" class="table-responsive">
<tr>
<td width="25%">
<asp:Label ID="Label2" runat="server" Text="Choose Expert Category"></asp:Label>
<asp:DropDownList ID="ddlcategory" runat="server" Height="16px" Width="138px" OnSelectedIndexChanged="State_Changed" AutoPostBack="true">
</asp:DropDownList>
</td>
<td width="25%"
<asp:Label ID="Label3" runat="server" Text="Choose Doctors"></asp:Label>
<asp:DropDownList ID="ddldoctors" runat="server" Height="16px" Width="138px" >
</asp:DropDownList>
</td>
</tr>
</table>