简体   繁体   中英

Primary Key in bound datagridview displayes negative numbers

I have a datagridview which is bound to table in sqlserver 2012 database. All fields are displayed correctly expect the primary key fields which is displayed as a negative numbers. 在此处输入图片说明

What is causing this?

<Global.System.Diagnostics.DebuggerNonUserCodeAttribute(),  _
     Global.System.CodeDom.Compiler.GeneratedCodeAttribute("System.Data.Design.TypedDataSetGenerator", "4.0.0.0")>  _
    Private Sub InitCommandCollection()
        Me._commandCollection = New Global.System.Data.SqlClient.SqlCommand(1) {}
        Me._commandCollection(0) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(0).Connection = Me.Connection
        Me._commandCollection(0).CommandText = "SELECT TSId, TCId, ElementNo, MSECId, UseInd, ElementDepth, ElementName, ChildInd"& _ 
            ", ElementValueDef, ElementValueNew, ElementPath, ElementPathFull, ElementValuePr"& _ 
            "efix, ElementValueLength, ElementValueIncrement, UpperControlElement1, UpperCont"& _ 
            "rolElementValue1, UpperControlElement2, UpperControlElementValue2, LowerControlE"& _ 
            "lement1, LowerControlElementValue1, LowerControlElement2, LowerControlElementVal"& _ 
            "ue2, AttributeName1, AttributeValue1, AttributeName2, AttributeValue2, Attribute"& _ 
            "Name3, AttributeValue3, AttributeName4, AttributeValue4, AttributeName5, Attribu"& _ 
            "teValue5 FROM dbo.XML_TEST_SET"
        Me._commandCollection(0).CommandType = Global.System.Data.CommandType.Text
        Me._commandCollection(1) = New Global.System.Data.SqlClient.SqlCommand()
        Me._commandCollection(1).Connection = Me.Connection
        Me._commandCollection(1).CommandText = "dbo.SP_XML_TEST_SET_FillByComboBoxes"
        Me._commandCollection(1).CommandType = Global.System.Data.CommandType.StoredProcedure
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@RETURN_VALUE", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.ReturnValue, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyENVId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyUId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyMTId", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestSetName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTestCaseName", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInterchangeSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyInstructionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyTransactionSeqNo", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementValueDef", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyElementPathFull", Global.System.Data.SqlDbType.NVarChar, 2147483647, Global.System.Data.ParameterDirection.Input, 0, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
        Me._commandCollection(1).Parameters.Add(New Global.System.Data.SqlClient.SqlParameter("@MyFetchRows", Global.System.Data.SqlDbType.Int, 4, Global.System.Data.ParameterDirection.Input, 10, 0, Nothing, Global.System.Data.DataRowVersion.Current, false, Nothing, "", "", ""))
    End Sub

Stored Proc:

    USE [UToolDb]

GO SET ANSI_NULLS ON GO SET QUOTED_IDENTIFIER ON GO ALTER PROCEDURE [dbo].[SP_XML_TEST_SET_FillByComboBoxes] -- Add the parameters for the stored procedure here @MyENVId AS INT, @MyUId AS INT, @MyMTId AS INT, @MyTestSetName AS NVARCHAR(MAX), @MyTestCaseName AS NVARCHAR(MAX), @MyInterchangeSeqNo AS INT, @MyInstructionSeqNo AS INT, @MyTransactionSeqNo AS INT, @MyElementValueDef AS NVARCHAR(MAX), @MyElementPathFull AS NVARCHAR(MAX), @MyFetchRows AS INT

AS BEGIN -- SET NOCOUNT ON added to prevent extra result sets from -- interfering with SELECT statements. SET NOCOUNT ON;

SET ROWCOUNT @MyFetchRows

DECLARE @sql NVARCHAR(MAX) = N'
SELECT     
TS.TCId,
TS.ElementNo,
TS.MSECId,
TS.UseInd,
TS.ElementDepth,
TS.ElementName,
TS.ChildInd,
TS.ElementValueDef,
TS.ElementValueNew,
TS.ElementPath,
TS.ElementPathFull,
TS.ElementValuePrefix,
TS.ElementValueLength,
TS.ElementValueIncrement,
TS.UpperControlElement1,
TS.UpperControlElementValue1,
TS.UpperControlElement2,
TS.UpperControlElementValue2,
TS.LowerControlElement1,
TS.LowerControlElementValue1,
TS.LowerControlElement2,
TS.LowerControlElementValue2,
TS.AttributeName1,
TS.AttributeValue1,
TS.AttributeName2,
TS.AttributeValue2,
TS.AttributeName3,
TS.AttributeValue3,
TS.AttributeName4,
TS.AttributeValue4,
TS.AttributeName5,
TS.AttributeValue5
FROM dbo.XML_TEST_SET TS 
LEFT JOIN dbo.XML_TEST_SET_OVERVIEW TSO 
ON TSO.TCId = TS.TCId '     
+ CASE WHEN (@MyENVId IS NOT NULL) THEN N'WHERE TSO.ENVId = @MyENVId ' ELSE 'WHERE TSO.ENVId IS NOT NULL '  END
+ CASE WHEN (@MyUId IS NOT NULL) THEN N'AND TSO.UId = @MyUId ' ELSE '' END
+ CASE WHEN (@MyMTId IS NOT NULL) THEN N'AND TSO.MTId = @MyMTId ' ELSE '' END
+ CASE WHEN (@MyTestSetName IS NOT NULL) THEN N'AND TSO.TestSetName = @MyTestSetName ' ELSE '' END 
+ CASE WHEN (@MyTestCaseName IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInterchangeSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyInstructionSeqNo IS NOT NULL) THEN N'AND TSO.TestCaseName = @MyTestCaseName ' ELSE '' END 
+ CASE WHEN (@MyTransactionSeqNo IS NOT NULL) THEN N'AND TSO.TransactionSeqNo = @MyTransactionSeqNo ' ELSE '' END
+ CASE WHEN (@MyElementValueDef IS NOT NULL) THEN N'AND TS.ElementValueDef = @MyElementValueDef ' ELSE '' END 
+ CASE WHEN (@MyElementPathFull IS NOT NULL) THEN N'AND TS.ElementPathFull = @MyElementPathFull ' ELSE '' END
+ N'ORDER BY TSO.TestSetName, TSO.TestCaseName, TS.ElementNo, TSO.InterchangeSeqNo, TSO.InstructionSeqNo, TSO.TransactionSeqNo;';

EXEC sp_executesql @sql, N' @MyENVId INT, @MyUId INT, @MyMTId INT, @MyTestSetName NVARCHAR(MAX), @MyTestCaseName NVARCHAR(MAX), @MyInterchangeSeqNo SMALLINT, @MyInstructionSeqNo SMALLINT, @MyTransactionSeqNo SMALLINT, @MyElementValueDef NVARCHAR(MAX), @MyElementPathFull NVARCHAR(MAX)', @MyENVId, @MyUId, @MyMTId, @MyTestSetName, @MyTestCaseName, @MyInterchangeSeqNo, @MyInstructionSeqNo, @MyTransactioNSeqNo, @MyElementValueDef, @MyElementPathFull; END

If you used the wizard to generate your dataset, It will automatically set the auto increment to -1, because it has no way of knowing what data is in the database it has no idea what number to use as an auto-increment, in which case -1 would be the best solution.

check out here social.msdn.microsoft.com

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM