Click here to Skip to main content
11,647,600 members (65,409 online)
Rate this: bad
good
Please Sign up or sign in to vote.
See more: VB Visual-Studio VB.NET
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
Posted 14-Nov-12 19:46pm

1 solution

Rate this: bad
good
Please Sign up or sign in to vote.

Solution 1

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..
  Permalink  

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

  Print Answers RSS
0 jyo.net 500
1 F-ES Sitecore 370
2 DamithSL 335
3 OriginalGriff 322
4 CPallini 290
0 OriginalGriff 1,277
1 jyo.net 994
2 DamithSL 946
3 Sergey Alexandrovich Kryukov 853
4 CPallini 760


Advertise | Privacy | Mobile
Web04 | 2.8.150804.2 | Last Updated 15 Nov 2012
Copyright © CodeProject, 1999-2015
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100