Hello everyone,i am new to c# programming and i am making a form app for my collage fest which has following details:
1.name of event(comboBox)
2.name of subevent(comboBox)
3.name of coordinator1(text box)
4.name of coordinator2(text box)
5.name of participent(text box)
5.price(text box)

my problem is when any event is selected then subevent list should come up and then on selection of subevent list,the name of both coordinators and price should come up using inbuild database named db.sdf but i do not know how to do it.although i have gone through various sites and a book "head first c#" (which give some details but it is for all the textbox together).please help and a small request to please explain me the method you give in a very simple manner.

thank you

ps:the pic i have uploded is just the starting ...i just want to understand in built database connection feature and methods to use
edit:sorry cannot upload the pic

Recommended Answers

All 15 Replies

Think of it in English first. What you need to do is:

Select Event
    -> Get Sub events for this event
    -> Populate Sub Event
    -> Get Possible co-ordinators
    -> Populate Coordinators
    -> Get Price Info (store)
Select Co-oridnators
Enter Participant
    -> Calculate Price and Display

This might translate into something like:

void EventSelection_OnSelected(...)
{
    PopulateSubEvents(selectedEventId);
    PopulateCoordinators(selectedEventId);
    EventPrice = GetPriceInfo(selectedEventId);
}

void PopulateSubEvents(int selectedEventId)
{
    subEventComboBox.Items.Clear(); // Always remember to clear first...

    List<Event> subEvents = dataStore.GetSubEvents(selectedEventId);
    foreach(Event subEvent in subEvents)
    {
        // Add to Sub Event combo box;
    }
}

void PopulateCoordinators(int selectedEventId)
{
    coordinatorsComboBox.Items.Clear();

    List<Coordinator> coordinators = dataStore.GetCoordinators(selectedEventId);
    foreach(Coordinator bossMan in coordinators)
    {
        // Add to Coordinators combo box
    }
}

PricingInformation GetPriceInfo(int selectedEventId)
{
    return dataStore.GetPriceInfo(selectedEventId);
}

To get the information out of the database you might have some queries in your datastore class that look like:

select EventId, EventName, EventDescription from Event where EventId = @SelectedEventId

You would need to add an SQL Parameter called @SelectedEventId Example :)

That's pretty much it :)

Thank you sir , i will get back to you with the result i got .

Sir, i on double-cliking the combobox i get the followin:

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
}

is this where i have to make changes..sorry for the such quesions but it takes me time to understand.
and also explain what exactly is comboBox suitable for.
thank you

The combobox is perfectly fine for this kind of task, but you will need to change the style to DropDownList

The event you listed is also the correct place for the code.

A combobox is suitable for anything where you have to select an item of data from a list. The different styles allow different things.
For example; DropDownList will allow you to drop down a selection and that all the items are read-only.
Whereas DropDown will allow you to edit the input section of the ComboBox, allowing you to select an item that isn't already contained.
Simple will display all the items at once and you select a single one. You can also type in the editable portion (at the top where your selection is displayed) and select one of your own choosing.

So in all cases, a combo box is suitable for any situation where you select a single item from a list.

commented: thanks +0

sir,in my database i have multiple entries with same event name associated with diffent subevent..
like
gaming dota
gaming nfs
gaming mario
i want the dropdown list to display distinct event name
for that i even tried using the < button in design section of form and added query but every time i run the program the list shows all event name even the repeated ones
please tell me where i am going wrong
thank you

Sorry but your question wasn't completely clear.

I am having trouble interpreting this:

in my database I have multiple entries with same event name associated with diffrent subevent..

In standard database design you would have something akin to a Main Event, with Sub Events linked as children.

So let's say you have the following tables; Event and Event Type (I think you have event and sub event confused in a structure sense. So in this case, rather than calling it an Event call it an EventType so this would be Gaming, Sport, Skill etc.

They could have a structure like:

Event
| EventId | EventName | EventDescription | EventTypeId |
--------------------------------------------------------
|    1    | MarioRace | Race with Mario  |      2      |
|    2    | Dota2     | Fight with Heros |      2      |
--------------------------------------------------------

EventType
| EventTypeId | TypeName |
--------------------------
|     1       |   Skill  |
|     2       |  Gaming  |
--------------------------

So what you can see here is that I've linked my events, to an event type, essentially reversing how you saw them.

What you can do here is;

select EventId, EventName, EventDescription from Event e
inner join EventType et on e.EventTypeId = et.EventTypeId
where et.TypeName = @EventType

@EventType is the parameter you pass in from your code. This will return you the information you need from the database that is of the selected EventType

sorry
actually by database is like this

event list
event name | subevent name | price
gaming       dota2          
gaming       nfs

I can see that your database design is much better

sir,i read your code but in that i cannot get the datastore variable in my program
did i miss something

It's something that you have to create yourself. It will be the interface between the database and your application.

sir,could you guid or explain me the functioning of compact database and its related interface that are needed to intract with windows form using c#

ps:i am using visual basic 2010 and mostly down form using drag and drop so i am trying to understand the coding behind it

thank you

I think you will have better luck if you ask about this specifically on the VB .NET forum :)

sir but i am using only c# window form and local database

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Form2k_app
{
    public partial class Form2k : Form
    {
       // DataSet ds = new DataSet();
        public Form2k()
        {
            InitializeComponent();


            //comboBox1.DataSource = "Event";
            //String[] st = even;
            //comboBox1.Items.AddRange= 
        }

        private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
        {
            string ID = comboBox1.SelectedValue.ToString();
            MessageBox.Show(ID);
           // SqlConnection myConnection = new SqlConnection("Database=event");
           /* string SqlDataPull = ("Select EventName FROM Event WHERE EventTypeID = '@ID' ORDER By EventName asc");
            myConnection.Open();
            SqlCommand cmd = new SqlCommand(SqlDataPull);
            cmd.CommandType = CommandType.Text;
            SqlDataReader dr = cmd.ExecuteReader();
            while (dr.Read())
            {
                SqlDataPull = dr[0].ToString();
                comboBox2.Items.Add(SqlDataPull);
            }
             */
        }

        private void Form2k_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'eventDataSet.Event' table. You can move, or remove it, as needed.
            this.eventTableAdapter.Fill(this.eventDataSet.Event);
            // TODO: This line of code loads data into the 'eventDataSet.EventType' table. You can move, or remove it, as needed.
            this.eventTypeTableAdapter.Fill(this.eventDataSet.EventType);

        }
    }
}

Oh right, you said you were using Visual Basic, I guess you actually meant Visual Studio? :)

Your SQL connection information and data readers should sit in a different class. This will keep your code tidy and re-usable.

I personally don't use the built in DataTables, but read everything in manually (similar to what you've done in SelectedIndexChanged).
What you should do here, is read all the data in to a class structure that you can store somewhere.

You should store your command string separately and you need to add the parameter to your command.

string cmdString = @"select EventName from Event where EventTypeId = @ID order by EventName asc";

You don't need to put your parameters in ' ' marks.

Once you have created your command, you add parameters as such:

using(SqlCommand cmd = new SqlCommand(cmdString, connection))
{
    cmd.Parameters.Add(new SqlParameter("EventName", int.Parse(ID));

    using(SqlDataReader reader = cmd.ExecuteReader())
    {
        while(reader.Read())
        {
            // Populate your class
            // Add populated class to a list somewhere
        }

        reader.Close();
    }
}

If you've not seen/used using before, a basic explanation is that it ensures Dispose is called on your object, preventing memory leaks. using can only be used where the object inherits from IDisposable

thanks a lot and thanks for spending your time on by problem
will get back and post my final code here :)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.