与Sql Server 2008链接的序列号递增1 [英] Serial Number Increment by 1 linked with Sql Server 2008

查看:87
本文介绍了与Sql Server 2008链接的序列号递增1的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的编码员,

我有一个简单的程序可以从sql server中获取数据并自动添加序列号.

表单名称-Form1

序列号-Textbox1.text

项目ID-Textbox2.text

项目名称-Combobox1.selected值

但是,当我运行程序时,它实际上以某种方式起作用.问题是当我第一次选择项目时,它将值设置为"1",如果我第二次选择项目,则它从sql中获取数据并将一个加到序列号的最大值中.我什至在下面给出了完整的代码.请帮忙.

Dear Coders,

I had a simple programming to fetch data from sql server and put serial number automatically.

Form name - Form1

Serial No - Textbox1.text

Project ID - Textbox2.text

Project Name - Combobox1.selected value

But When I run the program it actually works in a way. The Problem is when i select the project for the first time it is putting the value as "1" and if I select the Project for the Second time it fetches data from sql and add one to the max of serial number. I have Even Given the Full code Below. Please help.

Imports System.Data.SqlClient

Public Class Form1

    Dim con As New SqlConnection
    Dim myconstring As String = "Data Source=ETA-TESTSRVR;Initial Catalog=Procurement;Integrated Security=True"

    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

        TextBox2.Text = ComboBox1.SelectedValue
        con.ConnectionString = myconstring
        Dim da As New SqlDataAdapter("SELECT max(sl_no)+1 as max FROM e1 where project_id='" + TextBox2.Text + "'", con)
        Dim dt As New DataTable
        da.Fill(dt)
        If TextBox1.Text = "" Then
            TextBox1.Text = 1
        Else
            TextBox1.Text = dt.Rows(0).Item("max").ToString
        End If
    End Sub

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

        'TODO: This line of code loads data into the 'ProcurementDataSet.Project' table. You can move, or remove it, as needed.
        Me.ProjectTableAdapter.Fill(Me.ProcurementDataSet.Project)

    End Sub
End Class

推荐答案

解决了我自己..

只是这样更改了代码.

Solved This Myself..

Just Changed the Code Like this.

Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged

       TextBox2.Text = ComboBox1.SelectedValue
       con.ConnectionString = myconstring
       Dim da As New SqlDataAdapter("SELECT max(sl_no)+1 as max FROM e1 where project_id='" + TextBox2.Text + "'", con)
       Dim dt As New DataTable
       da.Fill(dt)
       TextBox1.Text = dt.Rows(0).Item("max").ToString
       If TextBox1.Text = "" Then
           TextBox1.Text = 1
       End If

   End Sub



无论如何,谢谢您的帮助编码器..



Anyways Thanks for your Help Coders..


这篇关于与Sql Server 2008链接的序列号递增1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆