简体   繁体   中英

How to add a if and else to SQL in c#

I'm still a beginner in c# and i'm using Visual Studio 2015. I have a problem because combobox only takes index. My combobox has a value of "admin, employee" so if the user select the 1 index (employee) it should be stored in the sql table of position as a "employee" but right now it stored only 0 and 1. * The position table has a datatype of nvarchar(20) *

private void aBtnSave_Click(object sender, EventArgs e)
    {
        string conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Do not delete\insertUserExample.mdf;Integrated Security=True;Connect Timeout=30";
        string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + this.aUserPosition.SelectedIndex + "');";
        SqlConnection sqlcon = new SqlConnection(conString);
        SqlCommand sqlcom = new SqlCommand(query, sqlcon);
        SqlDataReader sqlReader;

        try {
            sqlcon.Open();
            sqlReader = sqlcom.ExecuteReader();
            MessageBox.Show("User is Saved!");

            while (sqlReader.Read()) {

            }

            aTxtFirstName.Clear();
            aTxtLastName.Clear();
            aTxtUserName.Clear();
            aTxtPassword.Clear();
            aTxtContact.Clear();

        } catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }

You can add a variable and condition to change combobox selected index to your string as below :

var userTitleString = this.aUserPosition.SelectedIndex == 1 ? "employee" : "admin";
string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + userTitleString + "');";

Or you can bind your data to comboBox when adding item to it:

ComboBoxItem comboItem = new ComboBoxItem();
item2.Text = "Admin";
item2.Value = "Admin";

ComboBoxItem comboItem2 = new ComboBoxItem();
item2.Text = "Employee";
item2.Value = "Employee";

List<ComboBoxItem> items = new List<ComboBoxItem> { comboItem, comboItem2 };

this.yourComboBox.DisplayMember = "Text";
this.yourComboBox.ValueMember = "Value";

this.yourComboBox.DataSource = items;

private void aBtnSave_Click(object sender, EventArgs e)
    {
        ComboboxItem selectedString = (ComboboxItem)yourComboBox.SelectedItem;
        var userPosition= selectedString.Value;
        string conString = @"Data Source=(LocalDB)\MSSQLLocalDB;AttachDbFilename=C:\Do not delete\insertUserExample.mdf;Integrated Security=True;Connect Timeout=30";
        string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + userPosition + "');";
        SqlConnection sqlcon = new SqlConnection(conString);
        SqlCommand sqlcom = new SqlCommand(query, sqlcon);
        SqlDataReader sqlReader;

        try {
            sqlcon.Open();
            sqlReader = sqlcom.ExecuteReader();
            MessageBox.Show("User is Saved!");

            while (sqlReader.Read()) {

            }

            aTxtFirstName.Clear();
            aTxtLastName.Clear();
            aTxtUserName.Clear();
            aTxtPassword.Clear();
            aTxtContact.Clear();

        } catch (Exception ex) {
            MessageBox.Show(ex.Message);
        }

class ComboBoxItem
    {
        public string Text { get; set; }
        public string Value { get; set; }
    }

You can use code as below.

aUserPosition.SelectedItem.Text , You will get selected text ie "Employee" or "Admin".

 string query = "INSERT INTO userData (firstName,lastName,userName,password,contactNo,position) values('" + this.aTxtFirstName.Text + "', '" + this.aTxtLastName.Text + "', '" + this.aTxtUserName.Text + "', '" + this.aTxtPassword.Text + "', '" + this.aTxtContact.Text + "', '" + this.aUserPosition.SelectedItem.Text + "');";

If I didn't get you wrong, you want to get the text in combobox not the Index. Can you just try the code below ?

aUserPosition.GetItemText(this.aUserPosition.SelectedItem)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM