Isaac
Isaac

Reputation: 305

Concatenating multiple fields in C#

I am working on a Windows Forms application in which I have a form for an inmate that takes all the basic information about the inmate and his home address and everything.

I have two comboboxes, one is a parent combobox for State and the other one is a child combobox and that is for City. The values of the City combobox depend on the selected item from the State combobox.

For example, if the admin selects "State1" from State comboBox then values in the City comboBox appear like "City1", "City2", "City3" etc. and if the admin selects "State2" from State comboBox then the values in the City comboBox appear like "City4", "City5", "City6" etc.

I have this to store the value of the City comboBox:

sql_com.Parameters.AddWithValue("@City", City_ComboBox.SelectedItem);

All I want to know is, is there a way to concatenate multiple fields in this code. Say, I have multiple City comboBoxes by the name "City_ComboBox1", "City_ComboBox2" and "City_ComboBox3".

Is there a way I can do something like this:

sql_com.Parameters.AddWithValue("@City", City_ComboBox1.SelectedItem + City_ComboBox2.SelectedItem + City_ComboBox3.SelectedItem);

But I get an error.

Upvotes: 0

Views: 712

Answers (3)

Zanyar Jalal
Zanyar Jalal

Reputation: 1864

string.Concat

sql_com.Parameters.Add("@City", SqlDbType.NVarChar, 64).Value = string.Concat(City_ComboBox1.SelectedItem, City_ComboBox2.SelectedItem, City_ComboBox3.SelectedItem)

string.join

var join = ",";
    sql_com.Parameters.Add("@City", SqlDbType.NVarChar, 64).Value = string.Join(join  ,City_ComboBox1.SelectedItem, City_ComboBox2.SelectedItem, City_ComboBox3.SelectedItem)

Upvotes: 0

Cyber Progs
Cyber Progs

Reputation: 3873

Another easy way is to use the Interpolated Strings with the $ sign as the following:

string cityString = $"{City_ComboBox1.SelectedItem} {City_ComboBox2.SelectedItem} {City_ComboBox3.SelectedItem}";
sql_com.Parameters.AddWithValue("@City", cityString);

by adding $ in front of the string and inside this string just put any variable within the curly braces {variableName}

Upvotes: 0

jwatts1980
jwatts1980

Reputation: 7346

There are several ways you can do this. One is with the string.Format() method.

string cityString = string.Format("{0} {1} {2}", City_ComboBox1.SelectedItem, City_ComboBox2.SelectedItem, City_ComboBox3.SelectedItem);
sql_com.Parameters.AddWithValue("@City", cityString);

string.Format let's you create a template of sorts for how you want the string to look. You use the placeholder {#} with the number that corresponds to the index of the values you pass in. And you can use those placeholders multiple times within the template. Anything that you pass into the method after the first string value, gets converted into a string.

It is also worth noting that while AddWithValue is easy to use, it can cause problems as your application grows. It is recommended to use

sql_com.Parameters.Add("@City", SqlDbType.NVarChar, 64).Value = cityString;

But you will replace the NVarChar and 64 with values that match your database column set up. See https://www.dbdelta.com/addwithvalue-is-evil/

Upvotes: 3

Related Questions