Display Another Data from Other Field if the Data is Empty - mysql

Hello Everyone Good Afternoon,
I have 5 Fields in MySQL and there are:
ItemCode
Description
OrderQty
ApprovedQty
UoM
UnitPrice
Total
I cant explain it using proper words but I hope you will understand it by letting me show to you the table.
_______________________________________________________________
|ItemCode|Description|OrderQty|ApprovedQty|UOM|UnitPrice|Total|
---------------------------------------------------------------
|12345678|Ketchup |12.00 |0.00 |PC |1.00 |12.00|
|67891111|Soy Sauce |0.00 |12.00 |PC |1.00 |12.00|
---------------------------------------------------------------
Now you see it, I hope you can understand it now.
Here is my Question, I have a Code that will transfer the Data above into a Datagridview but theres a twist and that twist is the Question.
Here it is:
How can I Display the Data like this; If ApprovedQty is 0.00 then the Data in the OrderQty will display but if ApprovedQty is not 0.00 regardless of what data is in the OrderQty the ApprovedQty will be Show.
Something like this.
Based on the Table above
___________________________________________________
|ItemCode|Description|OrderQty|UOM|UnitPrice|Total|
---------------------------------------------------
|12345678|Ketchup |12.00 |PC |1.00 |12.00|
|67891111|Soy Sauce |12.00 |PC |1.00 |12.00|
---------------------------------------------------
Here is my code but it only shows the Approved Qty
Private Sub loadfinalpurch1()
Dim con1 As MySqlConnection = New MySqlConnection("server=localhost;userid=root;password=admin1950;database=inventory")
Dim sql1 As MySqlCommand = New MySqlCommand("Select ItemCode,Description,ApprovedQty,UoM,UnitPrice,Total from final_purch where PRnumber = '" & Label2.Text & "' and Added is NULL OR Added ='';", con1)
Dim ds1 As DataSet = New DataSet
Dim adapter1 As MySqlDataAdapter = New MySqlDataAdapter
con1.Open()
adapter1.SelectCommand = sql1
adapter1.Fill(ds1, "MyTable")
DataGridView1.DataSource = ds1.Tables(0)
con1.Close()
With DataGridView1
.RowHeadersVisible = False
.Columns(0).HeaderCell.Value = "Item Code"
.Columns(1).HeaderCell.Value = "Description"
.Columns(2).HeaderCell.Value = "Order Qty"
.Columns(3).HeaderCell.Value = "UOM"
.Columns(4).HeaderCell.Value = "Unit Price"
.Columns(5).HeaderCell.Value = "Total Amount"
End With
DataGridView1.Columns.Item(0).Width = 90
DataGridView1.Columns.Item(1).Width = 200
DataGridView1.Columns.Item(2).Width = 90
DataGridView1.Columns.Item(3).Width = 90
DataGridView1.Columns.Item(4).Width = 100
DataGridView1.Columns.Item(5).Width = 100
DataGridView1.ColumnHeadersDefaultCellStyle.Alignment = DataGridViewContentAlignment.BottomCenter
Dim checkBoxColumn As New DataGridViewCheckBoxColumn()
checkBoxColumn.HeaderText = "Tag"
checkBoxColumn.Width = 30
checkBoxColumn.Name = "checkBoxColumn"
DataGridView1.Columns.Insert(0, checkBoxColumn)
End Sub
I hope you get my point.Do I have something to do with the MYSQL Command? with My Code? I don`t know how to achieve this.
TYSM For Future Help

You don't do that condition anywhere, you could do it on your code or directly changing your query:
Select ItemCode,
Description,
IF(ApprovedQty = 0, OrderQty, ApprovedQty) as myApprovedQty,
UoM,
UnitPrice,
Total from final_purch where PRnumber = '" & Label2.Text & "' and Added is NULL OR Added ='';", con1

You should implement this in your SQL using a case statement.
https://dev.mysql.com/doc/refman/5.7/en/case.html
The above answers using the IF() function are correct, but in my opinion it is better to use the CASE-WHEN so as to match other database engines like Microsoft SQL Server and Oracle.

SELECT
ItemCode,
Description,
IF (ApprovedQty = 0, OrderQty, ApprovedQty),
UoM,
UnitPrice,
Total
FROM table

Related

Update database by getting the sum of a value in a specific column in listview and the value in my database

In my database i have a table named [productlist] and inside it are (Name),(Stock) (Brand),(Size)
i have a listview like this:
|Qty| Name | Brand | Size |
| 2 | Name 1| Brand 1| Size 1|
| 2 | Name 2| Brand 2| Size 2|
what i want to happen is when i click save button
the value of "Qty" in my listview will be added to the value of "Stock" in my database
(value of Qty in my listview) + (current value of Stock in my productlist table) = (Stock in my productlist table)
i dont how to start so i tried this code but it didnt work:
Private Sub btnSave_Click(sender As System.Object, e As System.EventArgs) Handles btnSave.Click
Dim MyConnection As New MySqlConnection
Dim MyTransaction As MySqlTransaction = Nothing
MyConnection.ConnectionString = "Server=localhost;Database=empiredb;Uid=root;"
MyConnection.Open()
MyTransaction = MyConnection.BeginTransaction
For Each item As ListViewItem In lvwProductPurchases.Items
Dim SQL As String = "update productlist set [Stock] = [Stock] + #0 where Name = #1 and Brand = #2 and Size = #3"
Dim CMD4 As New MySqlCommand
CMD4.Connection = MyConnection
CMD4.Transaction = MyTransaction
CMD4.CommandText = SQL
CMD4.Parameters.AddWithValue("#0", item.Text)
CMD4.Parameters.AddWithValue("#1", item.SubItems(1).Text)
CMD4.Parameters.AddWithValue("#2", item.SubItems(2).Text)
CMD4.Parameters.AddWithValue("#3", item.SubItems(3).Text)
CMD4.ExecuteNonQuery()
CMD4.Dispose()
Next
MsgBox("updated")
'try and catch i will put transactionrollback
End sub
You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[Stock] = [Stock] + '2' where Name = 'kahoy' and Brand = 'Brand 1' and Size = 'm' at line 1
I haven't done much work with VB.net, but I do see one problem with your MySQL query. In MySQL, placing brackets around column names to escape/refer to them is not valid syntax. It is valid in SQL Server, Access, and maybe a few other databases. I propose using the following syntax for your query:
Dim SQL As String = "UPDATE productlist SET Stock = Stock + #0 " _
"WHERE Name = #1 AND Brand = #2 AND Size = #3"

BindingSource Filter Datagridview, SQL Query

Using SQL Server Management Tools I created a view with the following
SELECT ID,
Groupname,
CONVERT(VARCHAR, TimeSub, 103) AS Date,
RIGHT(CONVERT(CHAR(20), TimeSub, 22), 11) AS Time,
TK,
CProblem,
Fix,
Username,
Closed_Date,
Reason
FROM dbo.log
I needed to seperate TimeSub into 2 fields 1 for Date and 1 for Time in AM/PM.
Using VB2012
Dim sqlSelectCommand As New SqlCommand(("Select * from log order by ID DESC"), CS)
Dim adapter As New SqlDataAdapter(sqlSelectCommand)
adapter.Fill(DTable)
BS.DataSource = DTable
DG1.DataSource = BS
BS.Filter = ("Date >= '" & Date.Now.ToString("dd/MM/yyyy") & "'")
'BS.Filter = ("Groupname = 'SW'")
What should happen is the datadridview should only show todays date based on the Date Column, it doesn't work, it shows all dates.
If I used the Groupname SW it sorts Groupname showing all records with SW in them. To me it's like the convert in the SQL view isn't working properly.
Any help appreciated.

VB.net MYSQL - Count Seconds in Database between 2 times

Hi im having a little problem with vb.net not sure which direction to head really.
The front end of the program is used to count seconds of a certain downtime of a process.
Database has the following table, and an example data:
status | startdate | stopdate | Seconds
Running 27/08/2012 11:10:11 27/08/2012 11:10:22 11
Running 27/08/2012 11:10:11 27/08/2012 11:10:22 11
Running 27/08/2012 12:14:27 27/08/2012 12:14:57 30
On the back end all im trying to do is populate a label with the sum of seconds using a datepicker for the date, and when doing the query i use the datepicker text and add the time on to the end of it, to no suprise it didnt work, also it only takes the start date into account, but not the stopdate as im trying to add up within that time frame, and im hopeing someone could help, this is the code i tried below:
Dim DTimePicker1 = DateTimePicker1.text & " 11:00:00"
Dim DTimePicker2 = DateTimePicker1.text & " 12:00:00"
cmd.Parameters.Clear()
cmd.CommandText = "SELECT SUM(seconds) FROM sqlccmlinestatus WHERE status = 'Running' and startdate BETWEEN #BEG AND #END"
cmd.Parameters.AddWithValue("#BEG", DTimePicker1)
cmd.Parameters.AddWithValue("#END", DTimePicker1)
Label70.Text = cmd.ExecuteScalar()
Any help would be great.
Many Thanks,
Pete
Dim D1 as Date = DateTimePicker1.Value
Dim D2 as Date = DateTimePicker2.Value
Dim DTimePicker1 as new Date(D1.Year, D1.Month, D1.Day, 11, 0, 0)
Dim DTimePicker2 as new Date(D2.Year, D2.Month, D2.Day, 12, 0, 0)
cmd.Parameters.Clear()
cmd.CommandText = "SELECT SUM(seconds) FROM sqlccmlinestatus WHERE status = 'Running' and startdate BETWEEN #BEG AND #END"
cmd.Parameters.AddWithValue("#BEG", DTimePicker1.ToString("yyyy-MM-dd HH:mm:ss.fff"))
cmd.Parameters.AddWithValue("#END", DTimePicker2.ToString("yyyy-MM-dd HH:mm:ss.fff"))
Label70.Text = cmd.ExecuteScalar()

Displaying and clearing values in label from database

Hello
I have a datatable called Customer with the attributes CustomerID, Firstname and Surname.
So far I have managed to display the labels with the correct customer connected to the correct customerID. As I write the CustomerID, it displays the firstname and the surname of the customer in the lables.
For example, If I write 5022(is a customerID) in the textbox, then lblFirstname = Jon and lblSurname = Snow. However, if I continue to write, say 502222, then it will still display Jon Snow. I want to make it appear only if it's exactly right, meaning that the labels will clear if I write a customerID that doesn't exists.
Here is my code so far:
Dim customerID As Integer
If txtCustomer.Text <> "" Then
CustomerID = CInt(txtCustomer.Text)
myCommand.CommandText = "Select firstname, surname from customer where CustomerID = " & CustomerID & ""
myAdapter = New MySqlDataAdapter(myCommand)
myTable = New DataTable
myAdapter.Fill(myTable)
If myTable.Rows.Count > 0 Then
lblFirstname.Text = myTable.Rows(0)("Firstname").ToString()
lblSurname.Text = myTable.Rows(0)("Surname").ToString()
End If
Else
lblFirstname.Text = ""
lblSurname.Text = ""
End If
myConnection.Close()
Any suggestions?
It seems that you have a problem with adding text to the labels.. Have you somewhere in your code assigned variables to your labels. If you have, remove them, I think that'll do the trick.

Convert a databound combobox's data to ProperCase in VB 2010

In VB 2010 I'm trying to show the DisplayMember in ProperCase without modifying my existing MySQL tables.
A snippet of my code looks like this:
Dim sql0 As String = "SELECT business, customer_id FROM customers WHERE cab = '1'"
Dim data0 As New MySqlDataAdapter(sql0, main.conn)
Dim ds0 As New DataSet
ds0.Clear()
data0.Fill(ds0, "customers")
cmb_company.DataSource = ds0
cmb_company.DisplayMember = "customers.business"
cmb_company.ValueMember = "customers.customer_id"
Ive tried something like this:
cmb_company.DisplayMember = StrConv("customers.business_name", VbStrConv.ProperCase)
but this only changes the case of the part in quotes, and not the actual returned values. I also tried querying from MySQL like this:
"SELECT UPPER(business), customer_id FROM customers WHERE cab = '1'"
but this fails to return the "business" field..
I'd love it if my combobox had proper case without changing everything.
I appreciate any suggestions!
Use the second one, but use an alias like this:
"SELECT UPPER(business) AS Business, customer_id FROM customers WHERE cab = '1'"
MySQL documentation:
A select_expr can be given an alias using AS alias_name. The alias is
used as the expression's column name and can be used in GROUP BY,
ORDER BY, or HAVING clauses. For example:
SELECT CONCAT(last_name,', ',first_name) AS full_name FROM mytable
ORDER BY full_name;
It seems a bit silly to use a while loop to achieve this especially after filling the table with an adapter. I suppose I could have just skipped the .fill and added the items manually through the loop, all while ProperCasing them. Anyway, here is my code:
Dim sql1 As String = "SELECT * FROM table"
Dim data1 As New MySqlDataAdapter(sql1, main.conn)
Dim ds1 As New DataSet
ds1.Clear()
data1.Fill(ds1, "table")
cmb_buyout.DataSource = ds1
Dim i As Integer = 0
While i < ds1.Tables(0).Rows.Count
Dim name As String = StrConv(ds1.Tables(0).Rows(i).Item(1).ToString, VbStrConv.ProperCase)
ds1.Tables(0).Rows(i).Item(1) = name
i += 1
End While
cmb_buyout.DisplayMember = "table.buyoutType"
cmb_buyout.ValueMember = "table.id"
To anyone trying to accomplish this, it is probably simpler to just change the case on the entries in the DB. I might just end up doing this!