populate text box with blank value

IHi could someone help me with this bit of code please, I want is to if the dropdown list value is 0 the textbox is populated with the number 0.

Similarly if the drop down list value = 1, the text box is populated with the number 1



private void PopulateSingleNumbers()
    {
        if (ddlNoAnswers.SelectedValue == 0)
        {
            tbText = "0";
        }
 if (ddlNoAnswers.SelectedValue == 1)
        {
            tbText = "1";
        }









getting errors
Operator '==' cannot be applied to operands of type 'string' and 'int'








    }

bailey1200Asked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

VenkatgviCommented:
Instead of using a function use the SelectedValueChanged event of the dropdownlist

sample code below

private void ddlNoAnswers_SelectedValueChanged(object sender, EventArgs e)
        {
            tbText .Text = comboBox1.Text;
        }
0
Roshan DavisCommented:
you can try either of the following
private void PopulateSingleNumbers()
{
if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
{
	tbText = "0";
}
if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
{
	tbText = "1";
}



 ======== OR ===========


private void PopulateSingleNumbers()
{
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
{
	tbText = "0";
}
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
{
	tbText = "1";
}

Open in new window

0
Roshan DavisCommented:
oops, some typo. Here is the corrected one
private void PopulateSingleNumbers()
{
	if (ddlNoAnswers.SelectedValue.CompareTo("0") == 0)
	{
		tbText = "0";
	}
	if (ddlNoAnswers.SelectedValue.CompareTo("1") == 0)
	{
		tbText = "1";
	}



OR


private void PopulateSingleNumbers()
{
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
	{
		tbText = "0";
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
	{
		tbText = "1";
	}

Open in new window

0
Cloud Class® Course: Python 3 Fundamentals

This course will teach participants about installing and configuring Python, syntax, importing, statements, types, strings, booleans, files, lists, tuples, comprehensions, functions, and classes.

Roshan DavisCommented:
or you can do this also
private void PopulateSingleNumbers()
{
     tbText = ddlNoAnswers.SelectedValue;

Open in new window

0
VenkatgviCommented:
Use the below code, wherever u need to update the value of dropdown to the textbox

   tbText .Text = ddlNoAnswers.Text;
0
bailey1200Author Commented:
hi thank, for that I forgor that the tb is in a repeater...
how can I work that in to the code so is exist in the current context??

Thanks

I have something like.....
 foreach (RepeaterItem item in gvPossibleAnswers.Items)
        {
            TextBox tb = (TextBox)item.FindControl("tbAnswer")    
        }
0
Roshan DavisCommented:
something like this?
foreach (RepeaterItem item in gvPossibleAnswers.Items)
        {
            TextBox tb = (TextBox)item.FindControl("tbAnswer");
            if(tb != null)
            {            
               tb.Text = ddlNoAnswers.SelectedValue; //or  ddlNoAnswers.Text
            }
        }

Open in new window

0
bailey1200Author Commented:
yeah something like that but I need to work that in to this...


private void PopulateSingleNumbers()
{
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0)
{
       tbAnswer= "0";
}
if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1)
{
       tbAnswer= "1";
0
Roshan DavisCommented:
something like this?
private void PopulateSingleNumbers() 
{ 
	TextBox tbAnswer;

	foreach (RepeaterItem item in gvPossibleAnswers.Items)
	{
		tbAnswer = item.FindControl("tbAnswer") as TextBox;
	}

	if(tbAnswer == null)
	{
		MessageBox.Show("Error");
		return;
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0) 
	{ 
		tbAnswer.Text = "0"; 
	} 
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1) 
	{ 
		tbAnswer.Text = "1"; 

Open in new window

0
Roshan DavisCommented:
or something like this?
private void PopulateSingleNumbers() 
{ 
	TextBox tbAnswer;

	foreach (RepeaterItem item in gvPossibleAnswers.Items)
	{
		tbAnswer = item.FindControl("tbAnswer") as TextBox;
		if(tbAnswer != null)
			break;
	}

	if(tbAnswer == null)
	{
		MessageBox.Show("Error");
		return;
	}
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 0) 
	{ 
		tbAnswer.Text = "0"; 
	} 
	if (Int32.Parse(ddlNoAnswers.SelectedValue) == 1) 
	{ 
		tbAnswer.Text = "1"; 

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C#

From novice to tech pro — start learning today.