Click here to Skip to main content
15,890,690 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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

C#
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)
    {
        //em = Session["email"].ToString();
        //string un = Session["user"].ToString();
        //Label1.Text = un;

        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"));
    }

}

ASP.NET
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>
Posted
Updated 5-May-16 4:48am
Comments
ZurdoDev 5-May-16 10:27am    
SO, change your sql so you only get 1 of each category. What's the actual problem?
Karthik_Mahalingam 5-May-16 10:27am    
ID seems unique. so distinct wont work.Adjust your DB Schema/ Data
F-ES Sitecore 5-May-16 10:29am    
"Doesn't work" means nothing. Is the parent drop down populated? When you select from it does the change event fire? Have you stepped through that change event to see what it does? Do you get the stateId? Is it greater than 0? Does it build your query? Is the query valid? Do you have something in the database that matches the query?

You have to learn to debug your code and step through it, you can't just dump hundreds of lines on a forum every time something doesn't work and expect someone to know what the problem is. It could be your data that we don't have access to.

http://www.codeproject.com/Articles/79508/Mastering-Debugging-in-Visual-Studio-A-Beginn

If you don't have distinct data then you can't show distinct values. You need to ensure each doctor is only entered into the table once.

1 solution

As several people have already advised, you should be preventing duplicates on the category.

Looking at this piece of code
C#
string query = "select Id, type from docreg";
you appear to have the categories stored as text against each doctor. This is not a good database design and will cause problems for you (as you have discovered)

Have a separate categories table e.g.
SQL
create table category
(
    id int identity(1,1),
    type varchar(30)
)

and ensure the categories are only entered on this table once (e.g. using primary key or checking for pre-existing type before insertion).
Then just store the id of the category on the docreg table. Better still have a link table that allows a doctor to cover more than one category
SQL
create table DocToCat
(
    DocId int,
    CategoryId int
)
Either way the query for populating ddlcategory becomes
C#
string query = "select Id, type from category";

If you absolutely cannot change your database schema then you need to change that line to
C#
string query = "select Max(Id), type from docreg group by type";
 
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