 |
| VB.NET 2002/2003 Basics For coders who are new to Visual Basic, working in .NET versions 2002 or 2003 (1.0 and 1.1). |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the VB.NET 2002/2003 Basics section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

February 26th, 2004, 06:06 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
cant update
Hello people,
Easy one for you clever guys out there:
My update code doesn't update!
The user clicks on a tree node that subsequently populates a grid.
Here is the code. Any ideas?
Private DAKPI As New SqlDataAdapter
Private Sub TreeView1_AfterSelect(ByVal sender As Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles TreeView1.AfterSelect
Dim dsnMain As New DataSet
Dim dgts_Main As New DataGridTableStyle
Dim i As Int16
'grdMain.Dispose()
Try
Select Case CInt(TreeView1.SelectedNode.Parent.Parent.Parent.T ag)
Case 76, 91 To 100, 102 To 107, 109 ' <==single week val
Dim comKPF As New SqlCommand
Dim comKPFU As New SqlCommand
Dim dsnUTmp As New DataSet
comKPF.Connection = CnnKpF
comKPF.CommandType = CommandType.Text
comKPF.CommandText = "select DISTINCT TOT_WeekID, TOT_Total from KpFTotals inner join KpFWeeks on (KPW_WeekID=TOT_WeekID) where TOT_weekid=@pTOT_WeekID"
comKPF.Parameters.Add("@pTOT_WeekID", SqlDbType.Int, 4, "TOT_WeekID")
comKPF.Parameters(0).Value = TreeView1.SelectedNode.Tag
DAKPF.SelectCommand = comKPF
Select Case TreeView1.SelectedNode.Parent.Parent.Parent.Tag
Case 42 To 44, 68 To 92, 109, 124 To 126, 130, 133 To 136
comKPIU = New SqlCommand("Update KpFTotals inner join KpFWeeks on (KPW_WeekID=TOT_WeekID) set TOT_Total = @pTOT_Total where TOT_WeekID = @pTOT_WeekID", CnnKpi)
comKPIU.CommandType = CommandType.Text
comKPIU.Parameters.Add(New SqlParameter("@pTOT_Total", SqlDbType.Real))
comKPIU.Parameters("@pTOT_Total").SourceVersion = DataRowVersion.Current
comKPIU.Parameters("@pTOT_Total").SourceColumn = "TOT_Total"
comKPIU.Parameters.Add(New SqlParameter("@pTOT_WeekID", SqlDbType.Int))
comKPIU.Parameters("@pTOT_WeekID").SourceVersion = DataRowVersion.Original
comKPIU.Parameters("@pTOT_WeekID").SourceColumn = "TOT_WeekID"
DAKPI.UpdateCommand = comKPIU
End Select
DAKPI.Fill(dsnUTmp)
dsnMain = dsnUTmp
grdMain.DataSource = dsnMain
grdMain.CaptionVisible = False
grdMain.Expand(0)
grdMain.NavigateTo(0, "table")
grdMain.RowHeadersVisible = False
'StyleDGrid(1, "KPFTotals", "TOT_Total")
End Select
Catch ex As System.NullReferenceException
End Try
End Sub
Private Sub grdMain_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles grdMain.CurrentCellChanged
DAKPF.Update(grdMain.DataSource)
End Sub
|
|

February 27th, 2004, 07:39 AM
|
|
Friend of Wrox
|
|
Join Date: Jul 2003
Posts: 142
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I'd try to check the dataset behind your datagrid; try getting a dataview of all modified rows;
Dim dv as new dataview(grdMain.DataSource.DataTables("TableName" ), "", "", DataViewRowState.ModifiedCurrent)
Loop through all rows in the dataview, seeing which rows have been changed - the update method of the dataadapter checks for the modified flag, then decides whether to to an INSERT, UPDATE or DELETE.
|
|

February 27th, 2004, 09:25 AM
|
|
Registered User
|
|
Join Date: Feb 2004
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
I don't know how to step through a dataview.
Let me refrase the problem:
I have a select command and a update command based on it.
These relate to a data adapter that fills a dataset that my datagrid looks at.
The user updates some columns and that should get written back to the SQL7 DB.
This all works fine when I set as the parameter a column that is not the primary key (so more than one record is returned to the grid).
But when I only want one record returned, I need to use the primary key for the parameter. But when I do this, the update code stops working.
Here are the two commands followed by the 3 relevant Subs:
Private Sub InitializeComponent()
...
'SqlSelectCommand1
'
Me.SqlSelectCommand1.CommandText = "SELECT TOT_TotalID, TOT_Total, TOT_WeekID FROM KpfTotals WHERE (TOT_WeekID = @pTO" & _
"T_WeekID)"
Me.SqlSelectCommand1.Connection = Me.SqlConnection1
Me.SqlSelectCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@pTOT_WeekID", System.Data.SqlDbType.Int, 4, "TOT_WeekID"))
'
'SqlUpdateCommand1
'
Me.SqlUpdateCommand1.CommandText = "UPDATE KpfTotals SET TOT_TotalID = @TOT_TotalID, TOT_Total = @TOT_Total, TOT_Week" & _
"ID = @TOT_WeekID WHERE (TOT_TotalID = @Original_TOT_TotalID); SELECT TOT_TotalID" & _
", TOT_Total, TOT_WeekID FROM KpiTotals WHERE (TOT_TotalID = @TOT_TotalID)"
Me.SqlUpdateCommand1.Connection = Me.SqlConnection1
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@TOT_TotalID", System.Data.SqlDbType.Int, 4, "TOT_TotalID"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@TOT_Total", System.Data.SqlDbType.Real, 4, "TOT_Total"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@TOT_WeekID", System.Data.SqlDbType.Int, 4, "TOT_WeekID"))
Me.SqlUpdateCommand1.Parameters.Add(New System.Data.SqlClient.SqlParameter("@Original_TOT_ TotalID", System.Data.SqlDbType.Int, 4, System.Data.ParameterDirection.Input, False, CType(0, Byte), CType(0, Byte), "TOT_TotalID", System.Data.DataRowVersion.Original, Nothing))
...
End Sub
Private Sub DataGrid1_CurrentCellChanged(ByVal sender As Object, ByVal e As System.EventArgs) Handles DataGrid1.CurrentCellChanged
SqlDataAdapter1.Update(DataSet11)
End Sub
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Doit()
End Sub
Sub Doit()
DataSet11.Clear()
Try
SqlDataAdapter1.SelectCommand.Parameters(0).Value = CInt(Me.TextBox1.Text)
Me.SqlDataAdapter1.Fill(Me.DataSet11)
Catch ex As Exception
MsgBox(ex.Message)
End Try
End Sub
The above works fine because the parameter for the select and update is not the primary/unique field of the table. But when I change the parameter to look at the primary/unique field of the table, it no longer updates!
Why??
|
|
 |