Click here to Skip to main content
15,881,380 members
Home / Discussions / C#
   

C#

 
GeneralRe: How to access an IEnumerable item using reflection Pin
Luc Pattyn23-Jul-08 16:13
sitebuilderLuc Pattyn23-Jul-08 16:13 
GeneralRe: How to access an IEnumerable item using reflection Pin
Luc Pattyn23-Jul-08 15:16
sitebuilderLuc Pattyn23-Jul-08 15:16 
AnswerRe: How to access an IEnumerable item using reflection Pin
Clive D. Pottinger23-Jul-08 14:43
Clive D. Pottinger23-Jul-08 14:43 
GeneralWithout explicit GetEnumerator Pin
Luc Pattyn24-Jul-08 2:19
sitebuilderLuc Pattyn24-Jul-08 2:19 
Questiondropdownlist data fetch Pin
scottichrosaviakosmos23-Jul-08 8:02
scottichrosaviakosmos23-Jul-08 8:02 
GeneralRe: dropdownlist data fetch Pin
nelsonpaixao23-Jul-08 14:43
nelsonpaixao23-Jul-08 14:43 
GeneralRe: dropdownlist data fetch Pin
scottichrosaviakosmos23-Jul-08 17:23
scottichrosaviakosmos23-Jul-08 17:23 
AnswerRe: dropdownlist data fetch [modified] Pin
nelsonpaixao24-Jul-08 13:39
nelsonpaixao24-Jul-08 13:39 
listen, what you request it´s not hard, but it takes lots of time to code and debug!!!
So, read the following and if you became stuck ask here in the forum anyone can help you there stay cool.Cool | :cool:
(i assume you mean that dropdownlist = combobox)



private void Form1_Load(object sender, EventArgs e)
{
textBox1.Visible = false;
comboBox1.Items.Add("ASP"); //0
comboBox1.Items.Add("C#"); //1
comboBox1.Items.Add("VB"); //2
comboBox1.Items.Add("Javascript"); //3
comboBox1.Items.Add("PHP"); //4
comboBox1.Items.Add("Other"); //5
}

private void comboBox1_SelectedIndexChanged(object sender, EventArgs e)
{
if (comboBox1.SelectedIndex == 5)
{
textBox1.Visible = true;
}
else
{
textBox1.Visible = false;
}
}

private void button1_Click(object sender, EventArgs e)
{
comboBox1.Items.Add(textBox1.Text);
}



Try that easy exemple but that´s not enought! The solution without using a database doesn´t exist!
How and where would you store the data from new values added to the textbox?
And as you can see, each time you load your application your combox will only have the old values not the new added items. You noticed it already, right?

What you have to do instead is this:

create a table (if you don´t have one already) to fill up the combobox, so when you press button new values from the textbox will be added to the table.


1) create table tech with: id int, name varchar(50)

id | name
1 | ASP
2 | c#
3 | VB
4 | HTML
5 | PHP
6 | C++
...etc

2) create a SQL store procedure to fill up/load your combox (trigger it with a load_form event)

SQL:
create procedure tech_all
as
begin
select id, name from dbo.tech
end
go


C#:
this is what i do to connect to database and fill combobox:

string CONNECTION_STRING = "..."; // you know this

SqlConnection con_sql = new SqlConnection(CONNECTION_STRING);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con_sql;
cmd.CommandText = tech_all; // the store procedure above
cmd.CommandType = CommandType.StoredProcedure;
con_sql.Open();
SqlDataAdapter sql_da = new SqlDataAdapter();
sql_da.SelectCommand = cmd;
DataTable dt = new DataTable();
sql_da.Fill(dt);
con_sql.Close();
COMBOBOX1.DataSource = dt;
COMBOBOX1.DisplayMember = "name"; // table tech variable
COMBOBOX1.ValueMember = "id"; // table tech variable


3)create a SQL SP to add values to your combobox (trigger it with button_click event)

SQL:
create procedure tech_add
@name as varchar(50)
as
begin
insert into dbo.tech (name)
values (@name)
end
go


C#:
this is what i do to connect to database and add values to combobox:
(check this part i didn´t have time to test)

string CONNECTION_STRING = "...";

SqlConnection con_sql = new SqlConnection(CONNECTION_STRING);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con_sql;
cmd.CommandText = tech_add; // the store procedure above
cmd.CommandType = CommandType.StoredProcedure;
//
command.Parameters.Add("@name", SqlDbType.VarChar).Value = textbox1.text; // your input textbox!!!
//
con_sql.Open();
SqlDataAdapter sql_da = new SqlDataAdapter();
sql_da.SelectCommand = cmd;
DataTable dt = new DataTable();
sql_da.Fill(dt);
con_sql.Close();
COMBOBOX1.DataSource = dt;
COMBOBOX1.DisplayMember = "name"; // table tech variable
COMBOBOX1.ValueMember = "id"; // table tech variable



Change anything you like to fit what you have in mind!
Good Luck Big Grin | :-D

modified on Thursday, July 24, 2008 7:54 PM

QuestionProblem booking timeslots within a range of dates Pin
Twyce23-Jul-08 7:18
Twyce23-Jul-08 7:18 
AnswerRe: Problem booking timeslots within a range of dates Pin
Luc Pattyn23-Jul-08 8:01
sitebuilderLuc Pattyn23-Jul-08 8:01 
QuestionWizard Control Suggestions Pin
jchalfant23-Jul-08 7:13
jchalfant23-Jul-08 7:13 
AnswerRe: Wizard Control Suggestions Pin
Pete O'Hanlon23-Jul-08 9:45
mvePete O'Hanlon23-Jul-08 9:45 
GeneralRe: Wizard Control Suggestions Pin
jchalfant23-Jul-08 11:15
jchalfant23-Jul-08 11:15 
GeneralRe: Wizard Control Suggestions Pin
Pete O'Hanlon24-Jul-08 10:07
mvePete O'Hanlon24-Jul-08 10:07 
QuestionXmlSchemaProvider XSD import problem Pin
Guinness4Strength23-Jul-08 5:52
Guinness4Strength23-Jul-08 5:52 
QuestionWindows Service - Access is denied. Pin
dataminers23-Jul-08 5:23
dataminers23-Jul-08 5:23 
AnswerRe: Windows Service - Access is denied. Pin
mark_w_23-Jul-08 5:46
mark_w_23-Jul-08 5:46 
GeneralRe: Windows Service - Access is denied. Pin
dataminers23-Jul-08 8:22
dataminers23-Jul-08 8:22 
AnswerRe: Windows Service - Access is denied. Pin
Frank Horn23-Jul-08 6:52
Frank Horn23-Jul-08 6:52 
AnswerRe: Windows Service - Access is denied. Pin
dataminers24-Jul-08 7:22
dataminers24-Jul-08 7:22 
Questionuse the loudspeakers with C# Pin
amityo23-Jul-08 5:08
amityo23-Jul-08 5:08 
AnswerRe: use the loudspeakers with C# Pin
mark_w_23-Jul-08 5:24
mark_w_23-Jul-08 5:24 
GeneralRe: use the loudspeakers with C# Pin
amityo23-Jul-08 5:28
amityo23-Jul-08 5:28 
GeneralRe: use the loudspeakers with C# Pin
mark_w_23-Jul-08 5:32
mark_w_23-Jul-08 5:32 
GeneralRe: use the loudspeakers with C# Pin
Luc Pattyn23-Jul-08 7:01
sitebuilderLuc Pattyn23-Jul-08 7:01 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.