user1062411
user1062411

Reputation: 65

Store a string based up dropdownlist selection

Basically I am making a webform where you will fill all of the textboxes out and then select a category from a dropdown and hit submit. Based upon which category you select should dictate what string the data from the textboxes is stored in. I'm on a novice level when it comes to C# and ASP.NET and something is off about my if statements but I can't figure out how to do them properly.

Code below:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

public partial class _Default : System.Web.UI.Page
{
string non_fiction;
string fiction;
string self_help;



protected void Page_Load(object sender, EventArgs e)
{


}


protected void Submit_btn_Click(object sender, EventArgs e)
{
    if (Cat_DropDownList.SelectedIndex = 0)
    {
        fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
    }

    if (Cat_DropDownList.SelectedIndex = 1)
    {
        non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;

    }

    if (Cat_DropDownList.SelectedIndex = 2)
    {
        self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
    }
}
}

Also to save another post I need to figure out a way to have these stored so I can call the "full" strings and add them to another one on another page.

Upvotes: 0

Views: 297

Answers (3)

L.B
L.B

Reputation: 116138

I would declare

StringBuilder non_fiction = new StringBuilder();
StringBuilder fiction = new StringBuilder();
StringBuilder self_help = new StringBuilder();

StringBuilder[] strings = null;

and use them as

protected void Page_Load(object sender, EventArgs e)
{
    strings = new StringBuilder[] { fiction, non_fiction, self_help };
}

protected void Submit_btn_Click(object sender, EventArgs e)
{
    strings[Cat_DropDownList.SelectedIndex].Append("Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text);
}

without ifs and switches

Upvotes: 1

Ravi Gadag
Ravi Gadag

Reputation: 15861

first you are missing == operator in if condition . you need to use == operator for comaparision

 if (Cat_DropDownList.SelectedIndex == 0)
    {
        fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
    }



 if (Cat_DropDownList.SelectedIndex == 1)
    {
        non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;

    }

    if (Cat_DropDownList.SelectedIndex == 2)
    {
        self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
    }

Upvotes: 1

BrokenGlass
BrokenGlass

Reputation: 160902

if (Cat_DropDownList.SelectedIndex = 0)
{
    fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}

= is assignment - you want a comparison with == instead - same applies to the other if statements. Also using string.Format() would make this statement much more readable (imo):

if (Cat_DropDownList.SelectedIndex == 0)
{
    fiction = string.Format("Title: {0} | Description: {1} | Price: {2} | Quantity: {3}", Titletxt.Text, Descriptiontxt.Text, Pricetxt.Text, Quantitytxt.Text);
}

Upvotes: 0

Related Questions