2011-12-08 5 views
2

저는 aspnet을 사용하는 초보자입니다. 자동 생성과 관련하여 문제가 발생합니다. 내 코드와 관련하여 문제가 있는지 알 수 있습니다. 요구 사항은 사용자가 클릭 할 때마다 lbl_categorycode.Text를 자동으로 생성해야합니다 추가하십시오. 미리 감사드립니다 : D자동 카테고리 ID 생성이 작동하지 않습니다.

Imports System.Data.SqlClient 
Imports System.Data 

Partial Class ADDCATEGORY 
    Inherits System.Web.UI.Page 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 

     Dim cmd As New SqlCommand 
     cmd.CommandText = "INSERT INTO CategoryTable (CategoryID, ProductCategory) VALUES ('" & lbl_categorycode.Text & "' , '" & txt_productcategory.Text & "')" 
     cmd.Connection = cn 
     cmd.Connection.Open() 
     cmd.ExecuteNonQuery() 
     cmd.Connection.Close() 
     MsgBox("Record Added") 
     Call gencategorycode() 
     Call clear() 

    End Sub 

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load 
     Call gencategorycode() 
    End Sub 
    Private Sub gencategorycode() 
    Dim cmd1 As New SqlCommand 
    Dim rdr As SqlDataReader 

    cmd1.Connection = cn 
    cmd1.Connection.Open() 
    cmd1.CommandText = "Select count(*) as s from CategoryTable " 
    rdr = cmd1.ExecuteReader 

    If rdr.HasRows = True Then 
     rdr.Read() 
     lbl_categorycode.Text = Format(CInt(rdr(0).ToString) + 1, "ACECATN000000") 

    Else 
     lbl_categorycode.Text = "ACECATN0000001" 
    End If 
    cmd1.Connection.Close() 
End Sub 
Sub clear() 
    txt_productcategory.Text = "" 
End Sub 

최종 클래스

답변

0

당신이 rowCount+1이 될 것입니다 가정 수 없습니다 다음 Id.let의 당신은 IDS 1,2,3와 데이터베이스에 4 개 레코드가 말한다 4. 그런 다음 ID가 2 인 레코드를 삭제한다고 가정하십시오. 그러면 3과 같은 개수가 생깁니다. 따라서 알고리즘에 따라 다음 레코드 ID는 4가됩니다. 데이터베이스에 이미 ID 4가있는 레코드가 있기 때문에 작동하지 않습니다.

그래서 할 수있는 일은 데이터베이스에 ID 열을 생성하게하는 것입니다. SQL Server 데이터베이스를 사용하는 경우 열에 대한 ID 사양을 사용할 수 있습니다. 또한 SQL 명령 SCOPE_IDENTITY (http://msdn.microsoft.com/en-us/library/ms190315.aspx)을 사용하여 마지막 ID 값을 삽입 할 수 있습니다.

0

이 ACECATN000000을 ACECATN000001에 복제 하시겠습니까?

Imports System.Data 
Imports System.Data.SqlClient 

Partial Class _Default 
    Inherits System.Web.UI.Page 
    Dim con As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename='|DataDirectory|example.MDF';Integrated Security=True;Connect Timeout=30;User Instance=True") 
    Dim cmd As New SqlCommand 

    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click 
     Dim selected As String = Left(DropDownList1.SelectedItem.ToString, 1) 
     Dim maximumfinaldata As String = "" 
     cmd = New SqlCommand("SELECT MAX(CategoryID) FROM CategoryTable WHERE (SUBSTRING(CategoryID,1,1)) ='" & Left(DropDownList1.SelectedItem.ToString, 1) & "'", con) 
     con.Open() 
     Dim result As SqlDataReader = cmd.ExecuteReader 
     While result.Read 
      ''it will reproduce C002 AS A MAX VALUE because the maxdatavalue from mycode was 002 
      Dim temp = result.GetValue(0) 
      Dim number = CInt(Right(result.GetValue(0), 3)) 
      If number > 0 And number < 9 Then 
       number += 1 
       maximumfinaldata = "00" & CStr(number) 
      ElseIf number > 9 And number < 99 Then 
       number += 1 
       maximumfinaldata = "0" & CStr(number) 
      ElseIf number > 99 Then 
       number += 1 
       maximumfinaldata = CStr(number) 
      End If 
     End While 
     MsgBox(maximumfinaldata) 
     con.Close() 
    End Sub 
End Class 

내가 수를 사용하지만 ID를 자동 생성하는 다른 옵션이,

+0

예를보세요? – Carisle

+1

카운트는 데이터베이스 행을 계산하지만 최대 값을 얻지는 못하지만 MAX를 사용하여 최대 데이터 값을 얻으라고 제안했습니다 –

+0

내 최신 예를 확인하여 도움이 될 수 있기를 바랍니다. –

관련 문제