Hi all & thanks in advance for any help you can give,
I'm an absolute beginner with programming and am trying to use the "ValueMember" string of a ComboBox List to set a string value, so I can then use that to send an email.
Here's what I've got so far:
DataTable table = new DataTable();
DataColumn colName = new DataColumn("Name");
DataColumn colEmailID = new DataColumn("Email");
table.Columns.Add(colName);
table.Columns.Add(colEmailID);
StreamReader reader = new StreamReader(System.Environment.CurrentDirectory + \\Email.txt);
do
{
table.Rows.Add(reader.ReadLine().Split(','));
}
while (reader.Peek() != -1);
cmbRecipient.DataSource = table;
cmbRecipient.DisplayMember = "Name";
cmbRecipient.ValueMember = "Email";
cmbRecipient.SelectedIndex = 0;
Ok, this works well at hiding the actual Email from view, but I can't find any way of actually assigning the string of "cmbRecipient.ValueMember", my code appears to only care what is displayed, not the actual value assigned.
Here's a few ways I've tried to assign the "cmbRecipient.ValueMember" to my program:
//string[] EmailAdd = cmbRecipient.ValueMember;
//string EmailAdd = cmbRecipient.ValueMember;
//string EmailAdd = cmbRecipient.Text;
//string EmailAdd = "Email";
None of these seem to work, it just keeps taking what is displayed, not what I've told it in code to set.
Any advice would be GREATLY appreciated.
Thanks for helping.
Andy
Loading
Manikavelu VelayuthamPosted Sep 6, 2010, 8:44 AM
Should be replaced as below
string EmailAdd = Convert.ToString(cmbRecipient.SelectedValue);
Posted Sep 6, 2010, 8:48 AM
you are AWESOME!!!!!!!!!!!!
Not only does that work, it actually makes sense to an "newby" like me.
THANKS HEAPS.
Cheers
Andy
Posted Sep 6, 2010, 8:40 AM
I've just inserted that code, but it returns the following error:
Error 1 Cannot implicitly convert type 'object' to 'string'. An explicit conversion exists (are you missing a cast?)
Can you assist with this error?
Thanks heaps.
Andy
ShankeyPosted Sep 6, 2010, 8:34 AM
You can change you databind binding code: But dont forget to mark my answer as accepted if it helped you :)
cmbRecipient.DataSource = table;
cmbRecipient.DataTextField = "Name";
cmbRecipient.DataValueField= "Email";
cmbRecipient.DataBind();
cmbRecipient.SelectedIndex = 0;
string EmailAdd = cmbRecipient.SelectedValue;
This will work :)