[英]Put data from MySQL to labels in a table in vb.net
I want to put sum of each month in a label in the table from MySQL database but i dont know to make it in the label cz i have lbl1, lbl2, ... lbl12. 我想将每个月的总和放在MySQL数据库表中的标签中,但我不知道要在标签cz中使它具有lbl1,lbl2,... lbl12。
My code: 我的代码:
connection.Open()
query = " SELECT SUM(Amount_income_table), MONTHNAME(Date_income_table)
FROM bacci.income_table
where year(Date_income_table)='" & LblYear.Text & "'
GROUP BY MONTHNAME(Date_income_table);"
Comand = New MySqlCommand(query, connection)
READER = Comand.ExecuteReader
While READER.Read
ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
End While
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Dispose()
End Try
this code will fill the chart but i also want to fill the labels with the same query. 此代码将填充图表,但我也想使用相同的查询填充标签。
you shouldn't have your label as lbl1, lbl2 and lbl3 etc. You should create them at run time. 您不应将标签标记为lbl1,lbl2和lbl3等。应在运行时创建它们。 Try this code ia blank project.
尝试将此代码用于空白项目。 Adapt your code from this example.
从此示例改编您的代码。 I like using list of objects but you can use an array too
我喜欢使用对象列表,但也可以使用数组
Dim LabelList As List(Of Label)
Sub LoadSumLabel()
LabelList = New List(Of Label)
For x = 1 To 12
Dim NewLabel As New Label
With NewLabel
.Name = DateAndTime.MonthName(x)
.Text = "0"
.AutoSize = True
.Left = 10
.Top = 10 + (LabelList.Count * NewLabel.Height)
End With
LabelList.Add(NewLabel)
Me.Controls.Add(LabelList.Item(LabelList.Count - 1))
AddHandler LabelList.Item(LabelList.Count - 1).Click, AddressOf Label_Click
'you can create a panel and add you control to it the same way. So if you resize the form you can have the scroll bars if it doesnt fit
'somepanel.controls(LabelList.Item(LabelList.Count - 1))
Next
End Sub
Private Sub Label_Click(sender As Object, e As EventArgs)
Dim thisLabel As Label = DirectCast(sender, Label)
MsgBox(thisLabel.Name, vbOKOnly, "Result")
End Sub
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles Me.Load
LoadSumLabel()
End Sub
Sub RunQuery()
connection.Open()
query = " SELECT SUM(Amount_income_table), MONTHNAME(Date_income_table)
FROM bacci.income_table
where year(Date_income_table)='" & LblYear.Text & "'
GROUP BY MONTHNAME(Date_income_table);"
Comand = New MySqlCommand(query, connection)
READER = Comand.ExecuteReader
While READER.Read
ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
LabelList.Find(Function(lb As Label) lb.Name = READER.GetString("MONTHNAME(Date_income_table)")).Text = READER.GetString("SUM(Amount_income_table)")
End While
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Dispose()
End Try
End Sub
Name your labels like this lblJanuary, lblFebruary, lblMarch .... lblDecember. 像这样命名您的标签lblJanuary,lblFebruary,lblMarch .... lblDecember。 And then you can easily solve your problem using this code :
然后,您可以使用以下代码轻松解决您的问题:
query = " SELECT SUM(Amount_income_table) as Total, MONTHNAME(Date_income_table)
FROM bacci.income_table
where year(Date_income_table)='" & LblYear.Text & "'
GROUP BY MONTHNAME(Date_income_table);"
Comand = New MySqlCommand(query, connection)
READER = Comand.ExecuteReader
While READER.Read
ChartIncomeYear.Series("Incomes").Points.AddXY(READER.GetString("MONTHNAME(Date_income_table)"), READER.GetString("SUM(Amount_income_table)"))
Me.Controls("lbl" & READER.GetString("MONTHNAME(Date_income_table)")).Text = READER.GetString("Total")
End While
connection.Close()
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
connection.Dispose()
End Try
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.