How do you reference data from different formview (ASP.NET) - sql

I'm a still new to the whole creating a website please understand and explain in simple terms.
Ok, so there's a formview called FormView1 and another formview called User. In FormView1 I have a data called CoinEarn and the other formview data called CurrentCoin (from different formview)
I want to update CurrentCoin through a session so I could put it on my update SQL (by Session("CurrentCoin") = CurrentCoin + CoinEarn) after clicking a button (which is placed inside FormView1 template). In the code I gave the button the command name "complete".
Since CurrentCoin is from a different SQL and formview I need to reference/call up the specific data (CurrentCoin) so I can make the session.
I'm not sure if my thought/solution on this problem is correct or not (by calling up data or make a session to update CurrentCoin) but the main objective is to make CurrentCoin += CoinEarn after clicking a button.
Please help me! I've been trying to find the answer but it's too complicated to understand in proper websites.
Protected Sub FormView1_ItemCommand(sender As Object, e As FormViewCommandEventArgs) Handles FormView1.ItemCommand
If e.CommandName = "complete" Then
Dim row As FormViewRow = FormView1.Row
Dim TaskstatusLabel As Label = CType(row.FindControl("TaskStatusLabel"), Label)
Dim what As String = TaskstatusLabel.Text
Dim TaskCoin As Label = CType(row.FindControl("CoinEarnLabel"), Label)
Dim Coin As String = TaskCoin.Text
''Session("CurrentcoinSession") = Coin + CurrentCoin
Dim TaskstatusLabelID As Label = CType(row.FindControl("TaskIDLabel"), Label)
Dim whatTaskID As String = TaskstatusLabelID.Text
Session("TaskIDSession") = whatTaskID
If TaskstatusLabel.Text = "Complete" Then
Session("TaskStatusSession") = "Incomplete"
ElseIf TaskstatusLabel.Text = "Incomplete" Then
Session("TaskStatusSession") = "Complete"
End If
SqlDsStatus1.Update()
FormView1.DataBind()
End If
End Sub

Use of session CurrentCoin optional:
Session("CurrentCoin") = ((Label)MyFormView.FindControl("lblCurrentCoin")).Text
Session("CurrentcoinSession") = Coin + Session("CurrentCoin")
get values from a form view

Related

How do I display an image from a DataGridView to another PictureBox in a new Windows Form?

What I'm trying yo do is once I clicked on a row in my DataGridView, clicked on the view button, and it opens a new window that displays all the data from the DataGridView to the textboxes. I already did the first part, but I don't know how to do it with the images (I have 2 image columns). Can someone help me out? I'm only starting, sorry. This is my code for the view button that opens up another window. The picture boxes are always blank.
Private Sub btnView_Click(sender As Object, e As EventArgs) Handles btnView.Click
If GunaDataGridView1.Rows.GetRowCount(DataGridViewElementStates.Selected) > 0 Then
data.txtID.Text = GunaDataGridView1.CurrentRow.Cells(0).Value.ToString
data.txtLN.Text = GunaDataGridView1.CurrentRow.Cells(1).Value.ToString
data.txtFN.Text = GunaDataGridView1.CurrentRow.Cells(2).Value.ToString
data.txtMN.Text = GunaDataGridView1.CurrentRow.Cells(3).Value.ToString
data.txtGen.Text = GunaDataGridView1.CurrentRow.Cells(4).Value.ToString
data.txtNum.Text = GunaDataGridView1.CurrentRow.Cells(5).Value.ToString
data.txtDOB.Text = GunaDataGridView1.CurrentRow.Cells(6).Value.ToString
data.txtAddress.Text = GunaDataGridView1.CurrentRow.Cells(7).Value.ToString
data.txtPlate.Text = GunaDataGridView1.CurrentRow.Cells(8).Value.ToString
data.txtVT.Text = GunaDataGridView1.CurrentRow.Cells(9).Value.ToString
data.txtVB.Text = GunaDataGridView1.CurrentRow.Cells(10).Value.ToString
data.txtVYM.Text = GunaDataGridView1.CurrentRow.Cells(11).Value.ToString
data.txtSP.Text = GunaDataGridView1.CurrentRow.Cells(12).Value.ToString
data.ownerPhoto.Image = GunaDataGridView1.CurrentRow.Cells(13).Value
data.carPhoto.Image = GunaDataGridView1.CurrentRow.Cells(14).Value
data.ShowDialog()
End If
End Sub
I saw another guy asked this question and another guy commented with this code line:
Dim bytes As Byte() = DataGridView1.CurrentRow.Cells(13).Value
Using ms As New MemoryStream(bytes)
ownerPP.Image = Image.FromStream(ms)
End Using
Dim bit As Byte() = DataGridView1.CurrentRow.Cells(14).Value
Using memory As New MemoryStream(bit)
carPhoto.Image = Image.FromStream(memory)
End Using
It works perfectly. I hope this helps someone out too.

Check if data exist in file

I need help. I want to check if user exists by entering their ic number and I want to display another rest of their data by using file in visual basic. Unfortunately, an error occurs while doing that. I need help. If the user exists, then It will display automatically name, email, address and so on but if a user doesn't exist, then it shows message box. Here I attached the image of the display screen and the code. Please help me. Thank you.
Public Class Form1
Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
Dim userFile As String = "C:\Users\HP\Desktop\userdata.txt"
Dim inputFile As String
If System.IO.File.Exists(userFile) = True Then
Dim objReader As New System.IO.StreamReader(userFile)
Dim intIc As Integer
Dim intCount As Integer = 0
Dim strName As String
Dim strEmail As String
Dim intPhoneNum As String
Dim strAdd1 As String
Dim strAdd2 As String
Dim intPostcode As String
Dim strState As String
Do While objReader.Peek() <> -1
intIc(intCount) = Convert.ToInt64(objReader.ReadLine())
If (intIc(intCount).Convert.ToInt64(objReader.ReadLine())) Then
strName(intCount) = objReader.ReadLine()
strEmail(intCount) = objReader.ReadLine()
intPhoneNum(intCount) = Convert.ToInt32(objReader.ReadLine())
strAdd1(intCount) = objReader.ReadLine()
strAdd2(intCount) = objReader.ReadLine()
intPostcode(intCount) = Convert.ToInt32(objReader.ReadLine())
strState(intCount) = objReader.ReadLine()
lblName.Text = strName
lblEmail.Text = strEmail
lblNum.Text = intPhoneNum
lblAdd1.Text = strAdd1
lblAdd2.Text = strAdd2
lblPostcode.Text = intPostcode
lblState.Text = strState
objReader.Close()
Else
MessageBox.Show("User Does Not Exist")
End If
intCount = intCount + 1
Loop
Else
MessageBox.Show("File Does Not Exist")
End If
End Sub
End Class
Your task, the easy way:
make a new project
add a DataSet to this new project
open the DataSet, in the properties call it something sensible
Right click the surface, add a new datatable, name it Person
Right click the datatable, add a column, name it IC. Right click, add column, name it Name. Keep going until you added all the fields you want to track(email,phone,address1 etc)
save the DataSet
open the form
show the datasources window (view menu.. other windows)
expand the nodes til you can see Person
click the drop down next to Person, switch from DataGridview to Details
drag Person onto the form. Text boxes, labels etc appear. In the tray at the bottom more things appear
add a textbox to the form and call it searchTextBox
add a search button to the form, double click it, add this line of code to the click handler:
personBindingSource.Filter = '[ic] LIKE '" & searchTextBox.Text & "'"
If personBindingSource.Count = 0 Then MessageBox.Show("No records")
double click the form background to add a form load event handler, put this line of code:
If IO.File.Exists("data.xml") Then .ReadXml("data.xml")
switch back to designer, single click the form background and switch to event properties of the form, add a handler to the form closing event:
.WriteXml("data.xml")
That's it, you now have a program that will open, read and fill the DataSet with data from the data.xml file, it will search it when you type something in the ic box, the text boxes use databinding to show values automatically, and when you close the program it will save updates data. The only task now is to load the xml file with data.
When the textboxes were added to the form you should also have seen a bar appear across the top with some left/right controls in and a green plus. Click the green plus, type some data in, click it again, type more data. Navigating back, if you're adding new data, will commit the data. If you're looking at existing data, editing it then navigating will commit it
After you added some data, you can search for existing data using the search box. When you've searched for a single value it should be the only thing shown and the nav will show "1 of 1". To get back to the mode where all data is showing, put a single asterisk in the search box and hit search; it should show the number records in the top bar and you can scroll them with the arrows.
If you already have lots of data in a file, like you use in your question, you can read it in a loop (like you do in your question, except don't use that code exactly cos it has loads of errors) as a one time thing and assign it into the datatable, or you can manipulate it directly into being XML in a text editor. This is easy to do if you have a capable text editor but I'll not offer any particular advice on it in case you don't have a large amount of existing data. Ask a new question if you do

Better way to update ComboBox dynamically?

I was wondering if anyone had a suggestion or tip to improve the following function of my code. Basically I'm making a Update/Modify Form which fills its fields from a table of a DataBase, everything so far works great until I arrive at the comboboxes.
I have three of them, role, status and gender. The problem is that when I loaded the current user's information like this:
Dim lstDatos As New ArrayList()
lstDatos = gestorUsuario.consultarUsuario(idUsuario)
txtNombre1.Text = lstDatos(0)
txtNombre2.Text = lstDatos(1)
txtApellido1.Text = lstDatos(2)
txtApellido2.Text = lstDatos(3)
cmbGenero.DisplayMember = lstDatos(4) 'HERE
txtCorreo.Text = lstDatos(5)
txtCedula.Text = lstDatos(6)
txtTelefono.Text = lstDatos(7)
cmbRol.Text = lstDatos(8) 'HERE
cmbEstado.Text = lstDatos(9) 'And Here
Only the user's current Rol, Estado and Gender could be selected, to workaround that I made the following:
Private Sub updateRol(sender As Object, e As EventArgs) Handles cmbRol.Click
actualizarComboBox()
End Sub
Which calls the actualizarFunction:
Private Sub actualizarComboBox()
cmbGenero.Items.Add("Masculino")
cmbGenero.Items.Add("Femenino")
cmbEstado.DataSource = gestorUsuario.consultarEstados
cmbEstado.DisplayMember = "nombre_estado"
cmbEstado.ValueMember = "id_estado"
cmbRol.DataSource = gestorRol.consultarRoles
cmbRol.DisplayMember = "nombre"
cmbRol.ValueMember = "id_rol"
End Sub
It kinda works to be honest but...the first time the comboBox is selected you can see a visible jump when it loads the other options, also when I click the combo it automatically loses the "placeholder" if you will, of the user's current information and goes right up to the first option.
So if I have:
*B
As my user's current cmbRol.Text and I click the ComboBox it flickers a bit and changes to:
*A
-B
-C
Where * represent the selected or highlighted option.
If anyone had any suggestions or tips I would be very grateful. Thanks a lot.
Replace
cmbGenero.DisplayMember = lstDatos(4) 'HERE
cmbRol.Text = lstDatos(8) 'HERE
cmbEstado.Text = lstDatos(9) 'And Here
for this:
cmbGenero.SelectedItem = lstDatos(4) 'SelectedItem is sufficient because cmbGenero is not binding to Data Base
cmbRol.SelectedValue = lstDatos(8) 'SelectedValue is required because cmbRol and cmbEstado is binding to Data Base
cmbEstado.SelectedValue = lstDatos(9)

How to reference controls located on different Tabs (VB.NET)

I have an application written in VB.NET that reads data from a file and displays the data on the screen.
Depending on the data in the file, the program has a TabControl with up to 3 tabs and each tab in turn has a DataGridView for displaying data. For example I have a TabControl that has a tab called "Saturday" and a tab called "Sunday".
The problem I am having is that when I read data from a file, the program displays all the data on the Saturday's tab grid because I am not sure how to reference the Grid on the Sunday tab.
To add the DataGridView I am using the following code:
Grid = New DataGridView
Grid.Dock = DockStyle.Fill
Grid.Name = "Grid" & TabControl.SelectedIndex
Grid.Tag = "Grid" & TabControl.SelectedIndex
And this is how I am reading the data in:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
If reader.Name = "cell" Then
y = y + 1
Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
End If
What I almost want to do is something like (pseudocode):
SelectedTab.Grid.Rows(i).Cells(y).Style.BackColor = Color.FromName(reader.ReadElementString("cell"))
However when I use the above code it complains:
'Grid' is not a member of 'String'
I hope you understand the issue. Let me know if you need clarification
Your code is a little unclear. However, it appears to me that the following line:
If reader.GetAttribute("controltype") = "Tab" Then
SelectedTab = reader.Name
End If
is creating at least one problem. It looks like you are attempting to refer to a Tabpage control by the string representation of its name, but unless I missed something, what that line is actually doing is trying to make a tabpage control type("SelectedTab") refer to a string type. If that is the case, then you will want to try this instead:
If reader.GetAttribute("controltype") = "Tab" Then
TabControl1.SelectedTab = TabControl1.TabPages(reader.name)
End If
It is a little hard to tell from the code you have posted, but that might get you headed down the right path.
++++++++++++
UPDATE: It appears from your code that you are naming each DGV control by appending the index of the tab on which it is located to the string "grid." I am going to assume that you are using a class member variable named "SelectedTab" to represent the current tab selected in the control. I will assume that at the top of your class you have done something like this:
'Form-or-class scoped memebr variables:
Private SelectedTab As TabPage
Private SelectedGrid As DataGridView
You should be able to refer to the active grid control using something like this:
Private Sub TabControl1_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles TabControl1.SelectedIndexChanged
' Set SelectedTab member variable to refer to the new selected tab page:
SelectedTab = TabControl1.SelectedTab
' Set the SelectedGrid to refer to the grid control hosted on the selected tab page:
SelectedGrid = TabControl1.SelectedTab.Controls("Grid" & TabControl1.SelectedIndex.ToString())
End Sub
From here, you should be able to use the member variable for SelectedGrid to refer to the grid present on which ever tab page is selected in your tab control.
It is challenging to address your concerns with only fragments of your code. If you have additional difficulties, please post more of your code, so we can better see what else is going on.
Hope that helps!
Okay, I would go about something like this. Maybe you can simply use a DataSet to load the XML data in one line (if they have been saved with DataSet.WriteXML before).
Dim ds As New DataSet
Dim p As TabPage
Dim gv As DataGridView
ds.ReadXml("F:\testdata.xml")
For i As Integer = TabControl1.TabPages.Count - 1 To 0 Step -1
TabControl1.TabPages.RemoveAt(i)
Next
For Each dt As DataTable In ds.Tables
p = New TabPage(dt.TableName)
gv = New DataGridView
' ... configure the gv here...
gv.AutoGenerateColumns = True
gv.Dock = DockStyle.Fill
' ...
gv.DataSource = dt
TabControl1.TabPages.Add(p)
p.Controls.Add(gv)
Next

should changing the parent of a listbox change the selected index?

You wouldn’t think so, but it does when the listbox is bound to a datasource (as far as I can see).
I’ve reduced the behaviour to the code below. The "if" line toggles between loading a list via data binding and loading a list “manually” (both use the same data table). In each case I set the selected index afterwards, and then change the parent form. With manual loading, the selected index is retained, with binding it is lost. I cannot see how this makes any sense – I don't see why changing the host form should alter any property of the list. Is this a bug?
Public Class Form1
Sub main() Handles Me.Load
Dim ListControl1 As ListBox = New ListBox
ListControl1.Parent = Me
Dim dt = New DataTable
dt.Columns.Add("intColourID")
dt.Columns.Add("strName")
dt.Rows.Add({1, "Red"})
dt.Rows.Add({2, "Green"})
dt.Rows.Add({3, "Blue"})
ListControl1.ValueMember = dt.Columns(0).ColumnName
ListControl1.DisplayMember = dt.Columns(1).ColumnName
If False Then
ListControl1.DataSource = dt
Else
For i = 0 To dt.Rows.Count - 1
ListControl1.Items.Add(dt.Rows(i)("strName").ToString)
Next
End If
ListControl1.SelectedIndex = 2
Dim z As Form = New Form
ListControl1.Parent = z
z.Show()
End Sub
End Class
The correct way of adding a control to a form is not to set its parent, but to add it to the Controls collection of the form. If I do it like this I do not get an exception (the three last lines commented out as you write in your comment).
Me.Controls.Add(ListControl1) ' Instead of ListControl1.Parent = Me