Search code examples
c#numericupdown

How to set my NumericUpDown on Database


For Example, My Item_Quantity on my Table is 50, then my NumericUpDown's Minimum will be 1 and 50? Im using C# and MySQL and I cant make it work.

Edit:

this is my code:

string MyConnectionString = "Server=localhost;Port=3307;database=invpos;Uid=root;Pwd=''";

    public void LoadGrid()
    {
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        connection.Open();

        try
        {
            MySqlCommand cmd = connection.CreateCommand();
            cmd.CommandText = "Select * from items";
            MySqlDataAdapter adap = new MySqlDataAdapter(cmd);
            DataSet ds = new DataSet();
            adap.Fill(ds);
        }
        catch (Exception)
        {
            throw;
        }
        finally
        {
            if (connection.State == ConnectionState.Open)
            {
                connection.Clone();
            }
        }
    }

    public void ComboBox()
    {
        MySqlConnection connection = new MySqlConnection(MyConnectionString);
        connection.Open();        
        MySqlCommand cmd = connection.CreateCommand();
        MySqlDataAdapter adap = new MySqlDataAdapter("Select Item_Name from items", connection);
        DataSet ds = new DataSet();
        adap.Fill(ds);
        comboBox1.DataSource = ds.Tables[0];
        comboBox1.DisplayMember = "Item_Name";
    }     
    private void Form4_Load(object sender, EventArgs e)
    {
        LoadGrid();
        ComboBox();

    }

I hope that someone can help me :(


Solution

  • This is what I want to happen: Example: Database: Item_Quantity = 50 and I want to set my NumericUpDown's Maximum Value into Item_Quantity's value :(

    As I understand, you don't know how to get the maximum value of NumericUpDown?

    Try this:

    int itemQuantity = numericUpDown1.Maximum; //Get maximum value of `NumericUpDown`
    int currentValue = numericUpdown1.Value; //Get current value of `NumericUpDown`
    

    Does it solve your problem?

    *Update

    try 
    {
       MySqlCommand cmd = connection.CreateCommand();
       cmd.CommandText = "Update <your table> set <column> = " + itemQuantity;
       //Use Command Parameter will be better.
       cmd.executeNonQuery(); //execute update.
    }
    catch (Exception ex)
    {
       //Catch exception
    }