I am creating an inventory management system in VB.Net whose basic functionality is to process incoming invoices. I want to insert data into my remote MySQL database, but only if the textbox is considered enabled. I have no problem inserting data into the database when I want all fields to be inserted. But I want the checkboxes to enable certain products and allow employees to enter only specific items. The checkboxes do enable and disable the text fields as needed, but when the data is inserted into the database it will enter null values for all product types, which I don't want it to do as it will mess up the invoicing system. Currently I've tried an if then statement but the problem I'm having is that it expects a textbox to be defined which is not enabled. The code I've tried is:
Public Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click Dim mysqlconn as new MySqlConnection ("ServerConnection") mysqlconn.Open() dim mysqlCmd as new MysqlCommand mysqlcmd.Connection = MysqlConn mysqlcmd.CommandText = "Insert into Table_Name (Column1,Column2,Column3) Values (@rec1,@Rec2,@Rec3)" If txtTextbox1.Enabled = True then mysqlcmd.Parameters.AddWithValue("@Rec1",Column1.text) End If If txtTextBox2.Enabled = True then mysqlcmd.Parameters.AddWithValue(@Rec2,Column2.text) End IF IF txtTextBox3.Enabled = True then mysqlcmd.Parameters.AddWithValue(@Rec3,Column3.text) End If
You can't just insert a column without rows. So if you want to insert one column, you have to insert all columns. If you don't want to add a value, the column can be NULL or an empty string, depending on the column in the database. You can also use
Using
and useAsync/Await
to remove the database work from the UIIf I understand your current design,
Table name