0

i just want to increment id inserted with the limit equal to other table

i have no problem with that

the only problem is that, i cant code increment on id until the limit

id column 2

1-----------0

2-----------5

3-----------0

and so on, like this, by cliking insert button,the id should increment one by one til the limit

can anyone please help me

    RecordDate.Text = MyFormat

    Dim antinull As Integer = Format(0)
    BadBeg.Text = antinull
    WarePcs.Text = antinull
    CustPcs.Text = antinull
    Returned.Text = antinull
    BadEnd.Text = antinull

   Try
        Str = "insert into BadWarehouseInv values("
        Str += Id.Text.Trim()
        Str += ","
        Str += """" & RecordDate.Text.Trim() & """"
        Str += ","
        Str += """" & BadBeg.Text.Trim() & """"
        Str += ","
        Str += WarePcs.Text.Trim()
        Str += ","
        Str += CustPcs.Text.Trim()
        Str += ","
        Str += Returned.Text.Trim()
        Str += ","
        Str += """" & BadEnd.Text.Trim() & """"
        Str += ")"
        Con.Open()
        Cmd = New OleDbCommand(Str, Con)
        Cmd.ExecuteNonQuery()
        Dst.Clear()
        Dad = New OleDbDataAdapter("SELECT * FROM BadWarehouseInv ORDER BY Id", Con)
        Dad.Fill(Dst, "stock")

        Con.Close()
    Catch ex As Exception
        MessageBox.Show("Could Not Insert Record!!!")
        MsgBox(ex.Message & " -  " & ex.Source)
        Con.Close()
larca
  • 9
  • 2
  • 7
  • Please consider [String.Format()](http://msdn.microsoft.com/en-us/library/fht0f5be%28v=vs.80%29.aspx): http://stackoverflow.com/questions/7054069/inserting-formating-characters-in-string-format – beatgammit Mar 22 '13 at 10:15
  • 1
    Better than that please consider a parameterised query! http://stackoverflow.com/questions/542510/how-do-i-create-a-parameterized-sql-query-why-should-i – Matt Wilko Mar 22 '13 at 10:16
  • @tjameson `String.Format` might be more readable, but just as susceptible to injection, you should be recommending using `SqlParameter`s instead. – Grant Thomas Mar 22 '13 at 10:17
  • @MattWilko - Yes! It took me a bit to realize that this was an SQL query. I saw a wall of string concatenations and [jumped to conclusions](https://www.youtube.com/watch?v=xRxqY4wuTHw). – beatgammit Mar 22 '13 at 10:18
  • those forums are very complicated, i want simple and understandable, it will be used for my redefense in system anaylys and development, i have no time left,plssssss :( – larca Mar 22 '13 at 10:25

1 Answers1

0

I'm not sure I understand your question but:-

Usually when you are writing to a database you have one column which is an Identity type and is the unique identifier in the table or Primary Key. This automatically increments when a new row is added.

So when you create a new row in the table you let the database engine worry about this and then use a technique to read this value back

If you don't want to use an identity column you could use a query to find the current highest number and then add one to it. This is not recommended because you may have two clients doing this at the same time and so one will fail when it tries to write to the database.

Community
  • 1
  • 1
Matt Wilko
  • 26,994
  • 10
  • 93
  • 143