Click here to Skip to main content
15,889,833 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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.

VB
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
Posted

1 solution

Solved This Myself..

Just Changed the Code Like this.

VB
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..
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900