|
-
May 31st, 2007, 07:20 AM
#1
Thread Starter
Addicted Member
[RESOLVED] DataGridView - Save Button click event replicates data in DGV
I have changed my code recently, so this post has changed
I have an app that is getting pretty large and one problem I am having is this:
One piece of my code on a save button exceutes a stored procedure and checks a textbox value against a database and looks for an entry in the table matching that text value. If it exists it does nothing....if it doesn't exist, it inserts the record into the database. After that is done, more code is executed and selects the data from the database and populates the datagrid. If I click the save button multiple times, the datagridview keeps increasing with records by 1 per save on a record that already exists in the table. When I close the app and reopen it, it only shows the single record though. Here is the code I am using
I have these declared at class level
Code:
Dim ds1 As New DataSet
Dim strSQLConn2 As String = "Data Source=HERCULES;Initial Catalog=Carcass2;Integrated Security=True"
Dim cn2 As New SqlConnection(strSQLConn2)
Code:
Private Sub KILSHEETBindingNavigatorSaveItem_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles bnBtnSave.Click
Dim strSQLConn1 As String = "Data Source=HERCULES;Initial Catalog=ShippingReportData;Integrated Security=True"
Dim cn1 As New SqlConnection(strSQLConn1)
Dim command As SqlCommand = New SqlCommand("sp_CarcCombo", cn1)
cn1.Open()
command.CommandType = CommandType.StoredProcedure
command.Parameters.AddWithValue("@KsID", txtKsId.Text)
command.ExecuteNonQuery()
cn1.Close()
cn1.Dispose()
Dim strSQLConn2 As String = "Data Source=HERCULES;Initial Catalog=Carcass2;Integrated Security=True"
Dim cn2 As SqlConnection = New SqlConnection(strSQLConn2)
Dim da1 As New SqlDataAdapter("SELECT ksID, ShipRptNo, Site, Barn, id, head, weight FROM t_groupdata WHERE ksID = " & txtKsId.Text, cn2)
da1.SelectCommand.CommandType = CommandType.Text
da1.AcceptChangesDuringFill = False
da1.Fill(ds1, "groupdata")
Me.GroupDataList.DataSource = ds1
Me.GroupDataList.DataMember = "groupdata"
End Sub
Private Sub btnSaveGroup_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSaveGroup.Click
Dim da1 As New SqlDataAdapter("SELECT ksID, ShipRptNo, Site, Barn, id, head, weight FROM t_groupdata WHERE ksID = " & txtKsId.Text, cn2)
Dim cb As New SqlCommandBuilder(da1)
da1.Update(ds1, "groupdata")
End Sub
[\code]
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_CarcCombo]
@KSId int
AS
IF (SELECT Count(*) AS RecCount FROM Carcass2.dbo.t_GroupData
LEFT JOIN Carcass2.dbo.Kilsheet
ON Carcass2.dbo.t_GroupData.ksid = Carcass2.dbo.Kilsheet.kilsheetid
WHERE
Carcass2.dbo.Kilsheet.kilsheetid = @KSid AND Carcass2.dbo.t_GroupData.shiprptno = Carcass2.dbo.Kilsheet.ship_report)
< 1
BEGIN
INSERT INTO Carcass2.dbo.t_groupdata (ShipRptNo, ksID, Site, Barn, ID, Head, Weight)
SELECT HeadShipped.ShipRptNo As ShipRptNo, Carcass2.dbo.Kilsheet.KilsheetID AS ksID, HeadShipped.Site, HeadShipped.Barn, ProdData.dbo.vOpenNonDupGroupIDs.LotNum AS ID, HeadShipped.Head AS Head, [Shipping Rpt].LiveWt AS weight
FROM (HeadShipped LEFT JOIN [Shipping Rpt] ON HeadShipped.ShipRptNo=[Shipping Rpt].ShipRptNo) LEFT JOIN ProdData.dbo.vOpenNonDupGroupIDs ON HeadShipped.Site=ProdData.dbo.vOpenNonDupGroupIDs.fSite AND HeadShipped.Barn=ProdData.dbo.vOpenNonDupGroupIDs.Barn LEFT JOIN Carcass2.dbo.Kilsheet ON Carcass2.dbo.Kilsheet.ship_report=Headshipped.ShipRptNo
WHERE Carcass2.dbo.Kilsheet.KilsheetID=@KSid
END
And here is the stored procedure I am using
Code:
set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go
ALTER PROCEDURE [dbo].[sp_CarcCombo]
@KSId int
AS
IF (SELECT Count(*) AS RecCount FROM Carcass2.dbo.t_GroupData
LEFT JOIN Carcass2.dbo.Kilsheet
ON Carcass2.dbo.t_GroupData.ksid = Carcass2.dbo.Kilsheet.kilsheetid
WHERE
Carcass2.dbo.Kilsheet.kilsheetid = @KSid AND Carcass2.dbo.t_GroupData.shiprptno = Carcass2.dbo.Kilsheet.ship_report)
< 1
BEGIN
INSERT INTO Carcass2.dbo.t_groupdata (ShipRptNo, ksID, Site, Barn, ID, Head, Weight)
SELECT HeadShipped.ShipRptNo As ShipRptNo, Carcass2.dbo.Kilsheet.KilsheetID AS ksID, HeadShipped.Site, HeadShipped.Barn, ProdData.dbo.vOpenNonDupGroupIDs.LotNum AS ID, HeadShipped.Head AS Head, [Shipping Rpt].LiveWt AS weight
FROM (HeadShipped LEFT JOIN [Shipping Rpt] ON HeadShipped.ShipRptNo=[Shipping Rpt].ShipRptNo) LEFT JOIN ProdData.dbo.vOpenNonDupGroupIDs ON HeadShipped.Site=ProdData.dbo.vOpenNonDupGroupIDs.fSite AND HeadShipped.Barn=ProdData.dbo.vOpenNonDupGroupIDs.Barn LEFT JOIN Carcass2.dbo.Kilsheet ON Carcass2.dbo.Kilsheet.ship_report=Headshipped.ShipRptNo
WHERE Carcass2.dbo.Kilsheet.KilsheetID=@KSid
END
How can I make sure the datagridview only shows the actual number of records in the t_groupdata table while still allowing user interaction of the datagridview as well? I want the end user to be able to manually add data.
Last edited by o9z1; Aug 10th, 2007 at 12:21 PM.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|