简体   繁体   中英

how do i convert combobox string values to integer to be parsed into mysql database in c#

as the heading says how do i convert say two string values (Active and In-Active) in a combobox upon selection of In-Active increases value of database column default value under column iActive from 0 to 1. so far i tried Convert.Toint32 and changed my stored procedure as

CREATE 
DEFINER=`root`@`localhost` 
PROCEDURE `sp_edit_user`
(
    pUserID INT(11),
    pEmployee_Name VARCHAR(15), 
    pUser_Name VARCHAR(15), 
    pRole VARCHAR(15),
    piActive INT(1)
)
BEGIN
    UPDATE t_user_auth SET Employee_Name = pEmployee_Name, User_Name = pUser_Name, Role = pRole, iActive = piActive + 1
    WHERE UserID = pUserID;
    END$$
DELIMITER ;

and code is

private void kryptonbtnSaveEdit_Click(object sender, EventArgs e)
{
    var connectionString = ConfigurationManager.ConnectionStrings["Pigen"].ConnectionString;
    using (MySqlConnection cnn = new MySqlConnection(connectionString))
        using (MySqlCommand cmd = cnn.CreateCommand())
        {
            try
            {
                cnn.Open();
                cmd.CommandText = "sp_edit_user";

                cmd.CommandType = CommandType.StoredProcedure;
          //      MessageBox.Show((comboBox1Status.SelectedValue), "");
                cmd.Parameters.AddWithValue("@pUserID", Convert.ToInt64(lbluserID.Text));
                cmd.Parameters.AddWithValue("@pEmployee_Name", txtboxEmploy.Text);
                cmd.Parameters.AddWithValue("@pUser_Name", txtboxuser.Text);
                cmd.Parameters.AddWithValue("@pRole", comboBox2Role.SelectedItem.ToString());
                cmd.Parameters.AddWithValue("@piActive", Convert.ToInt32(comboBox1Status.SelectedValue));

                //if(comboBox1Status.SelectedText != "Active")
                //{
                //    int m = Int32.Parse("In-Active");
                //}

what i notice is all values are increased from 0 to 1 even if selection is still on Active, probably from my stored procedure iActive + 1. how do i write this to accept In-Active as value 1.

Probably you can do this by conditional operator :

 cmd.Parameters.AddWithValue("@piActive", ((comboBox1Status.SelectedText == "Active") ? 1 : 0));

This will set @piActive as 1 if "Active" is selected else set @piActive as 0.

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