I am using the following code, but database is not getting updated:
Dim update As New SqlCommand("Update FAGR SET FAGRN=#FAGRN,FAGRU=#FAGRU WHERE FAGRC=#FAGRC", connection)
update.Parameters.Add(New SqlParameter("#FAGRC", SqlDbType.NChar, 3))
update.Parameters.Add(New SqlParameter("#FAGRN", SqlDbType.NVarChar, 50))
update.Parameters.Add(New SqlParameter("#FAGRU", SqlDbType.NChar, 3))
Dim i As Integer
i = 0
Do While i < DataGridView1.Rows.Count
Try
update.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).ToString
update.Parameters(1).Value = DataGridView1.Rows(i).Cells(1).ToString
update.Parameters(2).Value = DataGridView1.Rows(i).Cells(2).ToString
vartemp1 = update.ExecuteNonQuery()
i=i+1
Catch ex As Exception
MsgBox("Exception:" & vbCrLf & ex.Message)
Finally
Me.Close()
End Try
Loop
'IN A SIMILAR INSERT QUERY THE FIRST ROW OF GRID IS INSERTED IN DATABASE. THE OTHER RECORDS ARE NOT INSERTED BUT ALSO NO ERROR IS SHOWN.
First, make sure you're referring to the right columns:
Dim fagrc As DataGridViewColumn = DataGridView1.Columns("FAGRC")
Dim fagrn As DataGridViewColumn = DataGridView1.Columns("FAGRN")
Dim fagru As DataGridViewColumn = DataGridView1.Columns("FAGRU")
DataGridViewCell.ToString do not return the cell value. From reflector:
Public MustInherit Class DataGridViewCell
Public Overrides Function ToString() As String
Return String.Concat(New String() {"DataGridViewCell { ColumnIndex=", Me.ColumnIndex.ToString(CultureInfo.CurrentCulture), ", RowIndex=", Me.RowIndex.ToString(CultureInfo.CurrentCulture), " }"})
End Function
End Class
Do it like this:
Dim id As String = Nothing
For Each row As DataGridViewRow In DataGridView1.Rows
If (Not row.IsNewRow) Then
id = row.Cells(fagrc.Index).Value.ToString()
If (String.IsNullOrEmpty(id)) Then
Throw New ApplicationException("FAGRC missing.")
Else
update.Parameters(0).Value = id
update.Parameters(1).Value = row.Cells(fagrn.Index).Value.ToString()
update.Parameters(2).Value = row.Cells(fagru.Index).Value.ToString()
If (update.ExecuteNonQuery() <= 0) Then
Throw New ApplicationException("Could not update FAGRC with value '" & id & "'.")
End If
End If
End If
Next
Try to use the ForEach
'IN A SIMILAR INSERT QUERY THE FIRST ROW OF GRID IS INSERTED IN
DATABASE. THE OTHER RECORDS ARE NOT INSERTED BUT ALSO NO ERROR IS
SHOWN.
For Each dataGridViewRow As DataGridViewRow In DataGridView1.Rows
If dataGridViewRow.Index = 0 Then 'hint: this is your temporary textbox
Update.Parameters(0).Value = dataGridViewRow.Cells(0).ToString()
'Others Stuff
'vartemp1 = Update.ExecuteNonQuery()
Exit For
End If
Next
Hope it will helps.
Related
I have designed a SQL Server database app in which I fetch data from database and then insert in text boxes. I use a function to fetch data in data table from database and then I populate textboxes. I have to use this coding again and again:
If Dt.Rows.Count > 0 Then
TxtCust_Id.Text = Dt.Rows(0).Item(0)
TxtCust_City.Text = Dt.Rows(0).Item(1)
TxtCust_Area.Text = Dt.Rows(0).Item(2)
Else
TxtCust_Id.Text = String.Empty
TxtCust_City.Text = String.Empty
TxtCust_Area.Text = String.Empty
End if
Text boxes names changes according to query tables. My question is. Is it possible to make a function or procedure to populate data in text boxes from datatable using loops or any other method? Thanks in advance.
Shared Function ExecuteSelectDt(ByVal SelectCommand As String) As DataTable
Cmd = New SqlClient.SqlCommand
Sda = New SqlDataAdapter
' Dt = New DataTable
Try
DBConnection() ' Database connection details
Sda = New SqlDataAdapter(SelectCommand, Con)
Dim dt2 As New DataTable
Sda.Fill(dt2)
CloseConnection()
Return dt2
Catch ex As Exception
CloseConnection()
MsgBox(ex.Message)
Return dt
End Try
End Function
Private Sub TxtCust_Name_Leave(sender As Object, e As EventArgs) Handles TxtCust_Name.Leave
SQuery = "select Cust_Id, Cust_City, Cust_Area from TBLCustommers where Cust_Name= '" & TxtCust_Name.Text & "'"
Dt = Nothing
Dt = BM_Class_Liberary.SQLSereverDB.ExecuteSelectDt(SQuery)
If Dt.Rows.Count > 0 Then
TxtCust_Id.Text = Dt.Rows(0).Item(0)
TxtCust_City.Text = Dt.Rows(0).Item(1)
TxtCust_Area.Text = Dt.Rows(0).Item(2)
Else
TxtCust_Id.Text = String.Empty
TxtCust_City.Text = String.Empty
TxtCust_Area.Text = String.Empty
End if
End Sub
'create and populate list
dim txtBoxes as new List(of TextBox)();
for each ctrl as Control in Form.Controls
if ctrl.GetType() Is GetType(TextBox) then txtBoxes.Add(ctrl)
next
' then do this when you get DataTable
dim theRow as DataRow = dt.Rows(0); ' whatever logic you have to getting needed row
for each col as DataColumn in dt.Columns
' use system.linq
txtBoxes.First(function(tb) tb.Name = col.ColumnName).Text = theRow(col.ColumnName).ToString()
next
Note, when theRow(col.ColumnName) is DBNull.Value, ToString will return string.Empty, which is fine because .Text can't have Nothing
Also, I used First because I did it with the premise that each column has text box.
Or, using the dictionary. Even better, I think
'create and populate dictionary
dim txtBoxes as new Dictionary(of string, TextBox)();
for each ctrl as Control in Form.Controls
if ctrl.GetType() Is GetType(TextBox) then txtBoxes.Add(ctrl.Name, ctrl)
next
' then do this when you get DataTable
dim theRow as DataRow = dt.Rows(0); ' whatever logic you have to getting needed row
for each col as DataColumn in dt.Columns
txtBoxes(col.ColumnName).Text = theRow(col.ColumnName).ToString()
next
I have a dialog with a combobox listing the years an event was held.
Changing the year, changes the following list boxes
One list box 'called inEvent' shows all golfers the attended said event.
The other list box called 'available' that shows every golfer we have in our database that did not attend that years event
It has two buttons. One removes golfers from 'inEvent' and moves them to 'available'. This button works.
The other button does the opposite. It adds available golfers to the selected event year. But it gives me the error -
"The statement has been terminated. Cannot insert the value NULL into column 'intGolferEventYearID', table 'dbo.TGolferEventYears'; column does not allow nulls. INSERT fails."
Changing any line of code in VB results in a different error. So I think the error has to come from SQL itself which I don't know much about. Only other thing I can think of is the listbox is giving the wrong information.
Private Sub btnAddAuto_Click(sender As Object, e As EventArgs) Handles btnAddAuto.Click
Dim strInsert As String = ""
Dim cmdInsert As OleDb.OleDbCommand ' used for our Select statement
Dim dt As DataTable = New DataTable ' table we will load from our reader
Dim intRowsAffected As Integer
' open the DB
OpenDatabaseConnectionSQLServer()
' Build the select statement
strInsert = "INSERT INTO TGolferEventYears ( intGolferID, intEventYearID) Values (" & lstAvailable.SelectedValue & ", " & cboEvents.SelectedIndex + 1 & ")"
' Retrieve all the records
cmdInsert = New OleDb.OleDbCommand(strInsert, m_conAdministrator)
intRowsAffected = cmdInsert.ExecuteNonQuery()
' close the database connection and reload the form so the changes are shown
CloseDatabaseConnection()
frmEventsGolfers_Load(sender, e)
End Sub
I separated the data access code from the user interface. This will make it easy to remove data access entirely from the Form if you later desire.
Private Sub MoveGolfer(GolfID As Integer, YearID As Integer)
'If you keep your connections local you can be sure they are
'closed and disposed which is what the Using...End Using blocks do.
Using cn As New SqlConnection("Your connection string")
Using cmd As New SqlCommand("INSERT INTO TGolferEventYears ( intGolferID, intEventYearID) Values (#Golf, #Year;")
'Always use parameters to protect against Sql injection
cmd.Parameters.Add("#Golf", SqlDbType.Int).Value = GolfID
cmd.Parameters.Add("#Year", SqlDbType.Int).Value = YearID
cn.Open()
cmd.ExecuteNonQuery()
End Using
End Using
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim GolferID As Integer = CInt(lstAvailable.SelectedValue)
Dim Year As Integer = cboEvents.SelectedIndex + 1
Try
MoveGolfer(GolferID, Year)
Catch ex As Exception
'Catch a more specific exception and handle accordingly
MessageBox.Show(ex.Message)
End Try
End Sub
Since I don't really understand SQL I didn't realize my PK didn't have the IDENTITY tag added in the table. Adding this fixed my issue.
Here is the code I added
Dim strSelect As String
Dim cmdSelect As OleDb.OleDbCommand
Dim drSourceTable As OleDb.OleDbDataReader
Dim intNextHighestRecordID As
strSelect = "SELECT MAX(intDealerAutos) + 1 AS intNextHighestRecordID " &
" FROM TDealerAutos"
'Excute command
cmdSelect = New OleDb.OleDbCommand(strSelect, m_conAdministrator)
drSourceTable = cmdSelect.ExecuteReader
'Read result( highest ID )
drSourceTable.Read()
'Null? (Empty Table)
If drSourceTable.IsDBNull(0) = True Then
'Yes, start numbering at 1
intNextHighestRecordID = 1
Else
'No, get the next highest ID
intNextHighestRecordID = CInt(drSourceTable.Item(0))
End If
'Build the select statement
strInsert = "INSERT INTO TDealerAutos ( intDealerAutos, intDealerID, intAutoID)" &
"Values (" & intNextHighestRecordID & ", " & cboDealers.SelectedValue & ", " & lstAvailable.SelectedValue & ")"
I have a ComboBox that is created when the page is initialised
Dim CategoryCombo As New CustomControl.ComboCBx
With CategoryCombo
.Name = "MaintTypes_CatCombo"
End With
RegisterControl(MaintenanceTypes_Grid, CategoryCombo)
vToolBar.Items.Add(CategoryCombo)
vToolBar.Items.Add(TS_Separator)
and added to the toolbar
It is populated in the load event
Dim CatCombo As CustomControl.ComboCBx = MaintenanceTypes_Grid.FindName("MaintTypes_CatCombo")
With CatCombo
.IsNewRecord = False
.Width = 200
.ItemsSource = ReturnCategories.DefaultView
.SelectedValuePath = "ID"
.DisplayMemberPath = "Name"
.SelectedIndex = 0
End With
If the user navigates to another page and returns the selected value is returned to the selected index of 0. I can grab the last selected value before leaving the page but cannot find a way to set .SelectedValue when the page reloads
Data comes from
Private Function ReturnCategories() As DataTable
Try
CatDT = New DataTable
With CatDT.Columns
.Add("ID", GetType(Integer))
.Add("Name", GetType(String))
End With
With CatDT.Rows
.Add(0, "Select Category")
End With
Using vService As New Service1Client
strSQL = "SELECT Category_ID as 'ID', Category_Name as 'Name' FROM Maintenance_Categories "
strSQL += "WHERE Management_ID = " & Management_ID
strSQL += " ORDER BY Category_Name"
Dim DS As DataSet = vService.ReturnDataSetHAS(strSQL)
For Each Row As DataRow In DS.Tables(0).Rows
With CatDT.Rows
.Add(Row("ID"), ReturnText(Row("Name")))
End With
Next
End Using
Return CatDT
Catch ex As Exception
EmailError(ex)
Return Nothing
End Try
End Function
Any ideas?
Thanks
Found a workaround - return the index from the DataTable and set the .SelectedIndex of the ComboBox with that
In case someone has deleted the selected item prior to returning to the page check the row exists first
Dim vIndex As Integer = 0
If Not CurrentCategory = 0 Then
Dim vRow As DataRow = CatDT.Select("ID = '" & CurrentCategory & "'").FirstOrDefault()
If Not vRow Is Nothing Then
vIndex = CatDT.Rows.IndexOf(vRow)
End If
End If
With CatCombo
.IsNewRecord = False
.Width = 200
.ItemsSource = ReturnCategories.DefaultView
.SelectedValuePath = "ID"
.DisplayMemberPath = "Name"
.SelectedIndex = vIndex
End With
So, Access database has two columns 1st is seller name (SELLER) and second is it's code (CODE). Access database is database with all SELLER CODEs.
DGV is populated from other source and consists of some of CODEs from Access database.
DGV has one column with codes.
I would like to create and populate new column (SELLERNAME) in DGV with SELLER names based on DGV codes and Access database CODEs.
I am going to give an example since it's hard for me to explain this better :
DGV : Column 0, 1st value = 0055, in Access database code 0055 corresponds to the name John
so I would like to put name "John" in DGV Column1, 1st value, nest to "0055" and so on
Here is my code so far :
DataGridView1.Columns.Add("SELLERNAME", "SELLERNAME") '1
Dim cn As OleDb.OleDbConnection
Dim cmd As OleDb.OleDbCommand
Dim odr As OleDb.OleDbDataReader
Dim strSQL As String
cn = New OleDb.OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\SCodes.MDB")
strSQL = "SELECT SELLER, CODE FROM SCBASE"
cn.Open()
cmd = New OleDb.OleDbCommand(strSQL, cn)
odr = cmd.ExecuteReader(CommandBehavior.CloseConnection)
Do While odr.Read
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(1).Value = odr.GetValue(1).ToString()
Next
Loop
I don't know what to do next after line For i As Integer = 0 To DataGridView1.Rows.Count - 1.
EDIT :
I think I am close but I need help with one line of code :
Using sqlconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\SCodes.mdb")
Using sqlcmd = New OleDbCommand("Select SELLER, CODE From SCBASE Where CODE = #CODE ", sqlconn)
sqlcmd.Parameters.AddWithValue("#CODE ", DataGridView1.Rows(i).Cells(0).Value) 'HOW TO ADD THIS LINE IN LOOP BELOW
sqlconn.Open()
Dim result = sqlcmd.ExecuteReader()
Do While (result.Read())
For i As Integer = 0 To DataGridView1.Rows.Count - 1
DataGridView1.Rows(i).Cells(1).Value = (result("DOBAVLJAC"))
Next
Loop
End Using
End Using
I had no success with a trial and error. I am trying to put this line sqlcmd.Parameters.AddWithValue("#CODE ", DataGridView1.Rows(i).Cells(0).Value) in loop below but I am getting all sorts of error.
Yes, you are very close. You just need to .Add the Parameter outside the loop and then set its .Value inside the loop like this
Using sqlconn = New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=w:\SCodes.mdb")
sqlconn.Open()
Using sqlcmd = New OleDbCommand("Select SELLER, CODE From SCBASE Where CODE = ?", sqlconn)
sqlcmd.Parameters.Add("?", OleDbType.VarWChar, 255)
sqlcmd.Prepare()
For i As Integer = 0 To DataGridView1.Rows.Count - 1
sqlcmd.Parameters(0).Value = DataGridView1.Rows(i).Cells(0).Value
Using result = sqlcmd.ExecuteReader()
If result.Read() Then
DataGridView1.Rows(i).Cells(1).Value = result("SELLER")
End If
End Using
Next
End Using
End Using
I am new to VB.Net and Window Forms. I am doing a filtering on a table, so that the words entered in searching machine can be used to show the infos(row) that are relevant only. I will explain my situation after I showed my codes:
Private Sub findTextBox2_TextChanged(sender As Object, e As EventArgs) Handles findTextBox2.TextChanged
Dim viewPerson As DataView
viewPerson = New DataView(Data.ds.Tables("PersonInfos"))
If Not String.IsNullOrEmpty(Me.findTextBox2.Text) Then
Dim person As String = FilterListBox("PersonInfos", Me.findTextBox2.Text)
viewPerson.RowFilter = "ID in (" & person & ") "
Else
viewPerson.RowFilter = ""
End If
DataGridView2.DataSource = viewPerson
con.Close()
End Sub
After this filtering, all the infos (all the columns) frm the table "PersonInfos" will be showed. If I just want the column of "Last Name", "Age" and "Nationality" from the table (in a .mdb file) be shown only. What should i include in the codes.
I have search online and didn't manage to find a solution to my problem. Any help will be much appreciated. Thank you in advance.
This is what inside the method FilterListBox:
Private Function FilterListBox(ByVal TableName As String, ByVal tbText As String) As String
Dim IDs As String = String.Empty
If tbText.Contains("*") Then
If Not tbText.StartsWith("*") Then
tbText = "^" + tbText
End If
tbText = tbText.Replace("*", ".*")
If tbText.Contains("?") Then
tbText = tbText.Replace("?", ".")
End If
For Each row As DataRow In data.ds.Tables(TableName).Rows
If System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase) Then
If IDs <> "" Then IDs &= ","
IDs &= row.Item("ID")
Dim dfd As String = row.Item("Name").ToString()
Dim a As Boolean = System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
End If
Next
Else
For Each row As DataRow In data.ds.Tables(TableName).Rows
If row.Item("Name").ToString.StartsWith(tbText, StringComparison.CurrentCultureIgnoreCase) Then
If IDs <> "" Then IDs &= ","
IDs &= row.Item("ID")
Dim dfd As String = row.Item("Name").ToString()
Dim a As Boolean = System.Text.RegularExpressions.Regex.IsMatch(row.Item("Name"), tbText, System.Text.RegularExpressions.RegexOptions.IgnoreCase)
End If
Next
End If
If IDs <> "" Then
Return IDs
Else
Return "1=2"
End If
End Function
Try this
Dim viewPerson As DataView
Dim da As New OleDbDataAdapter
Dim tbl as New DataTable
da = New OleDbDataAdapter("SELECT Last Name, Age, Nationality FROM PersonInfos", con)
da.Fill(tbl)
viewPerson = New DataView(tbl)