Click here to Skip to main content
15,893,564 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Deal all,
Plz help me out regarding verifying error in code.I want to get incremented number in textbox every time form load. Number remain defaultly 1 but not getting incremented.

code is :
VB
Private Sub mainpage_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim conn As SqlConnection
        Dim cmd As New SqlCommand
        Dim data_base As String = " Data Source=CHET-PC\sqlexpress;Initial Catalog=invoice;Integrated Security=True"
        conn = New SqlConnection(data_base)
        Dim dr As SqlDataReader
        cmd = New SqlCommand("Select MAX(IS NULL(no),0)+1) from billdetail1", conn)
        dr = cmd.ExecuteReader
        dr.Close()
        TextBox1.Text = dr("no").ToString()
    End Sub
Posted
Updated 19-Dec-16 6:35am

It looks like your code increments the result from the select statement, but that doesn't update it in the database. You need to explicitly update the value.
 
Share this answer
 
Comments
chetanp322 3-May-12 13:34pm    
i have other code that updates all my field in database
Espen Harlinn 4-May-12 7:15am    
5'ed!
Your query:
SQL
Select MAX(IS NULL(no),0)+1) from billdetail1

gives a result:

[no column name]
value


Next in the code, you are trying to read results from column named: no
VB
TextBox1.Text = dr("no").ToString()

Take a look at result (above table) and tell me where is a field named no?

So... replace your query with:
SQL
Select ISNULL(MAX([no]),0)+1 AS [no] FROM billdetail1


Read more about: IDENTITY[^]

[EDIT name="second look"]
I see one potential risk in the way you are trying to get the number of billdetail. Let say 2 users in the same time open a form. Your query will return for them billdetail [no] = 5. What's happen when they will try to save data? You'll get duplicates.
That's why the most important is to set identity (increment) - go to above link, example A - for the column named [no]
Of course, before save data, you can re-query a [no] to get max+1, but it not guarantee non-duplicates numbers.[/EDIT]
 
Share this answer
 
v2
Comments
Maciej Los 3-May-12 16:21pm    
Chetanp322, please mark your question as "solved".
chetanp322 4-May-12 6:06am    
afterr making changes in code as suggested by you...still it is not incrementing...plz help as i m beginner
Maciej Los 5-May-12 12:10pm    
If you are a beginner, you need to read about IDENTITY (above link).
If you're talking about an auto numbering field in a DataTable then have a look at this: http://msdn.microsoft.com/en-us/library/system.data.datacolumn.autoincrement.aspx[^]
 
Share this answer
 
Comments
chetanp322 3-May-12 12:59pm    
plz suggest me what error does my code have???
Dim sqlText As String = "SELECT MAX(no) FROM billdetail1 "

Dim con3 As New SqlConnection("Data Source=CHET-PC\sqlexpress;Initial Catalog=invoice;Integrated Security=True")
Dim command As New SqlCommand(sqlText, con3)

Try
con3.Open()
Dim pcount As String = Convert.ToString(command.ExecuteScalar())
If pcount.Length = 0 Then

TextBox1.Text = "1"
Else
Dim pcount1 As Integer = Convert.ToInt32(pcount)
Dim pcountAdd As Integer = pcount1 + 1
TextBox1.Text = pcountAdd.ToString()

End If

Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
con3.Close()
End Try
 
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