Datagridview from mysql: Simple inventory stocks - vb.net

I'm trying to create a simple inventory stock and i'm having a hard time to get the remaining stocks base on the input and output from mysql.
What I really need is to subtract the total quantity of input.quantity - output.quantity where input.material is equal to output.material and if the total.quantity is below the safety stocks the datagridview will highlight the row.
conn = New MySqlConnection
conn.ConnectionString = "server=localhost;userid=root;password=1234;database=inventory"
Dim searchquery As String = "Select input.DeliveryDate as 'Delivery Date',input.Material, (sum(input.Quantity) - sum(output.Quantity)) as 'Remaining Stocks' where rawmaterialsinput.Material = output.Material, safetystandard.safetystocks from inventory.input, inventory.output, inventory.safetystandard"
Dim commander As New MySqlCommand(searchquery, conn)
Dim adapter As New MySqlDataAdapter(commander)
inventorydata.Clear()
adapter.Fill(inventorydata)
inventoryDGV.DataSource = inventorydata
as of now this is the codes I'm trying but there's no hope. Please help
I need to have 4 column in datagridview with Delivery Date, Rawmaterials, Remaining Stocks, Safety Stocks.

conn.Open()
conn = New MySqlConnection
conn.ConnectionString = "server=localhost;userid=root;password=SOUTHEAST;database=reportingsystem"
Dim searchquery As String = "Select reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials as 'Raw Material', reportingsystem.rawmaterialswarehouseandrawmaterials.safetystocks as 'Safety Stocks', (select sum(reportingsystem.rawmaterialsinput.Quantity) - sum(reportingsystem.rawmaterialsoutput.Quantity) from reportingsystem.rawmaterialsinput, reportingsystem.rawmaterialsoutput where reportingsystem.rawmaterialsinput.RawMaterial = reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials ) as 'Remaining Stocks' from reportingsystem.rawmaterialswarehouseandrawmaterials" ' JOIN reportingsystem.rawmaterialsinput.RawMaterial ON reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials = reportingsystem.rawmaterialsinput.RawMaterial ORDER BY reportingsystem.rawmaterialswarehouseandrawmaterials.Rawmaterials"
Dim commander As New MySqlCommand(searchquery, conn)
Dim adapter As New MySqlDataAdapter(commander)
monitoringdata.Clear()
adapter.Fill(monitoringdata)
MonitoringDGV.DataSource = monitoringdata
conn.close()
in this code only 3 column is selected. Hope it's help

Related

Get Difference of two dates retrieve from MS Access database using visual basic

Dim da As New OleDb.OleDbDataAdapter("SELECT ITemID, ItemName, ItemDescription, ItemQuantity, ItemBorrowedDate, ItemReturnDate FROM BorrowedItem order by ID DESC", conn)
Dim dt As New DataTable
da.Fill(dt)
OverDueList.DataSource = dt.DefaultView
Dim ItemReturenedDate As New Date
Dim DateToday As Integer
Dim DateDiff As New Date
ItemReturenedDate = dt.Rows(0)("ItemReturnDate")
DateDiff = DateDiff(DateInterval.Day, ItemReturenedDate, DateTimePicker1.Value)`
I tried that code to generate output but my knowledge was not that good. i need help, it could be a great help if someone would notice it
And if you looking to send all rows to say a grid?
then this:
Dim strSQL As String =
"Select ITemID, ItemName, ItemDescription, ItemQuantity, ItemBorrowedDate, ItemReturnDate,
(ItemReturnDate - ItemBorroedDate) as MyDays
FROM BorrowedItem order by ID DESC"
dim dt as DataTable = MyRst(strSQL)
So, you can return MyDays for each row, and then say send the data table to a data grid view.
And I get VERY tired very fast typing over confection string and command objects, so you can use this routine (over and over).
Function MyRst(strSQL As String) As DataTable
Dim rstData As New DataTable
Using conn As New OleDbConnection(My.Settings.AccessDB)
Using cmdSQL As New OleDbCommand(strSQL, conn)
conn.Open()
rstData.Load(cmdSQL.ExecuteReader)
End Using
End Using
Return rstData
End Function
Try something like this:
Dim ItemReturenedDate As Date
Dim Days As Long
ItemReturenedDate = dt.Rows(0)("ItemReturnDate")
Days = DateTimePicker1.Value.Subtract(ItemReturenedDate).TotalDays

ASP TableAdapters. Getting row data from a SQL parameter query

Trying to get my head round ASP.NET DALs and TableAdapters and I'm hoping somebody can help me with getting the column data when using SQL query parameters.
If I have a TableAdapter with a query that returns one or more rows, I can get row and column data from the datatable no problem. For a simple query which returns one row I can use:
SQL: SELECT firstName, lastName from Names WHERE ID=1
Dim MyAdapter1 As New ProjectTableAdapters.NamesTableAdapter1
Dim f As String = MyAdapter1.GetNames.Rows(0)("firstName")
Dim l As String = MyAdapter1.GetNames.Rows(0)("lastName")
However if use a TableAdapter which has a query with a parameter I keep getting exceptions thrown. I'm sure this is simple, if someone can point me in right direction (vb if possible). Thanks a lot.
SQL: SELECT firstName, lastName from Names WHERE ID=#ID
Dim ID As Integer = 1
Dim MyAdapter2 As New ProjectTableAdapters.NamesTableAdapter2
Dim f As String = MyAdapter2.GetNames(Rows(0)("firstName"),ID) [throws exection]
Edit:
I think I have it working now - my bad syntax...
Dim f As String = MyAdapter2.GetNames(ID).Rows(0)("firstName")
Public Function MyFunction() As DataSet
Dim conString As String = "Connection String"
Dim conn As New SqlConnection(conString)
Dim da As New SqlDataAdapter()
Dim cmd As New SqlCommand With {
.Connection = conn,
.CommandText = "SELECT firstName, lastName from Names WHERE ID=#ID",
.CommandType = CommandType.Text}
cmd.Parameters.Add("#ID", SqlDbType.Int).Value = 1
da.SelectCommand = cmd
Dim ds As DataSet = New DataSet()
conn.Open()
da.Fill(ds)
conn.Close()
Return ds
End Function

Getting DevExpress Chart Series Values From Array VB.NET

I was trying to set DevExpress Chart Series from Data inside SQL Table.
Everything went fine except that the chart takes only the last attribute from the last series.
My code is:
con.Open() 'it opens SQL Connection
For i = 0 To (CheckedListBoxControl1.CheckedItems.Count - 1) 'list from ListBox
Lst.Add(CheckedListBoxControl1.CheckedItems(i).ToString) 'Putting Data in Array List
Dim Strl As String = String.Format("Select * from VPRogressCumulative where fname like '{0}' and lname like '{1}' order by id, no, CAST('1.' + date AS datetime)", ComboBox1.Text, Lst(i))
Dim sqlCom As New SqlCommand(Strl)
sqlCom.Connection = con
Dim myDA As SqlDataAdapter = New SqlDataAdapter(sqlCom)
Dim myDataSet As DataSet = New DataSet()
myDA.Fill(myDataSet, "VPRogressCumulative")
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Next
con.Close()
I believe my Problem is in:
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
ser.ArgumentDataMember = "VPRogressCumulative.Date"
ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
Last two lines are giving the same series new attributes which I wasn't able to resolve the issue.
Your problem is here:
ChartControl1.DataSource = myDataSet
ChartControl1.DataAdapter = myDA
In each iteration of cycle you are creating the new DataSet and it's overrides previous DataSource in your ChartControl1. Your must use Series.DataSource instead of ChartControl.DataSource. Also you can use DataTable instead of DataSet.
Here is example:
'Your code:
'Dim myDataSet As DataSet = New DataSet()
'myDA.Fill(myDataSet, "VPRogressCumulative")
'ChartControl1.DataSource = myDataSet
'ChartControl1.DataAdapter = myDA
'Replace with this:
Dim myDataTable As DataTable = New DataTable("VPRogressCumulative")
myDA.Fill(myDataTable)
Dim ser As New Series(Lst(i), ViewType.Line)
ChartControl1.Series.Add(ser)
'Your code.
'ser.ArgumentDataMember = "VPRogressCumulative.Date"
'ser.ValueDataMembers.AddRange(New String() {"VPRogressCumulative.Cumulative"})
'Replace with this:
ser.DataSource = myDataTable
ser.ArgumentDataMember = "Date"
ser.ValueDataMembers.AddRange(New String() {"Cumulative"})

Update Access database records by column, row known

This is what I've got so far :
Dim myCONN As OleDbConnection = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\Baza.mdb")
Dim cmd1 = New OleDbCommand("SELECT ID FROM Baza WHERE NAZIV=#XXNAZIV")
cmd1.Parameters.AddWithValue("#XXNAZIV", TextBox2.Text)
cmd1.Connection = myCONN
myCONN.Open()
Dim result = cmd1.ExecuteReader()
While (result.Read())
Dim rowx As Integer = GetTextOrEmpty(result("ID"))
End While
I've found the row (rowx) in which I would like to change values in 20 corresponding columns (namesID : NAZIV, SIFRA,...). Data is already presented in textboxes (textbox1...), but I don't know how to finish this code with UPDATE and how to insert changed values back to Access.
Dim cmdText As String = "UPDATE Baza SET NAZIV=#XXNAZIV Where ID=SomeId"
Using con = new OleDbConnection("PROVIDER=Microsoft.ACE.OLEDB.12.0;Data Source = h:\Baza.mdb")
Using cmd = new OleDbCommand(cmdText, con)
con.Open()
cmd.Parameters.AddWithValue("#XXNAZIV",TextBox2.Text)
cmd.ExecuteNonQuery()
End Using
End Using
This should help you to solve your problem, of course you will have to pass ID parameter to query also.
Reference

VB Count query result in a textbox

I want to populate the result of an SQL count query on a Access database in a textbox showing how many results there are. For example I have a code number inputted into the database 200 time, i want the textbox to show 200.
Heres my code so far:
ID = DataGridView1.CurrentRow.Cells(0).Value
fn = DataGridView1.CurrentRow.Cells(1).Value
ln = DataGridView1.CurrentRow.Cells(2).Value
SKU = DataGridView1.CurrentRow.Cells(4).Value
FC = DataGridView1.CurrentRow.Cells(5).Value
Dim countNoTwo As String = "SELECT COUNT skuNo FROM table WHERE skuNo = " & SKU & ""
Dim connection As New OleDbConnection(duraGadgetDB)
Dim dataadapter As New OleDbDataAdapter(countNoTwo, connection)
Dim ds As New DataSet()
connection.Open()
dataadapter.Fill(ds, "dura")
connection.Close()
txtbox1.Text
How do i bind the result of the dataset to the txtbox1.Text?
First, do not use string concatenation to build sql commands
(reason=Sql Injection + Parsing problems)
Second, if your command returns only one single result you could use
ExecuteScalar
Third, use the Using statement to be sure that your connection and
commands are correctly disposed after use
Dim countNoTwo As String = "SELECT COUNT skuNo FROM table WHERE skuNo = ?"
Using connection As New OleDbConnection(duraGadgetDB)
Using command As New OleDbCommand(countNoTwo, connection)
command.Parameters.AddWithValue("#sku", SKU)
connection.Open()
Dim result = Convert.ToInt32(command.ExecuteScalar())
txtbox1.Text = result.ToString
End Using
End Using
Try this
Dim dt As DataTable = ds.Tables(0)
txtbox1.Text = dt.Rows(0).Item(0).ToString()