.. Checking for Exsisting Sales Invoice Number in SQL Database ..

Mahmood Haji

Member
Joined
Jan 31, 2011
Messages
5
Programming Experience
1-3
..

Hi All,

im Facing a Problem in Having no idea on How i can prompt an Display Massage "This Sales Invoice Number Has Already Been Taken ,, Kindly enter another Number" .. im using Visual Studio 2008 + SQL Server Express Database ..

The Process Mechanism:
- When the user entered any number in Sales Invoice [SIO] it saves automatically in the Database ..

- after the prevoius transaction ,, another user may use the System and may enters another any value ,, but in case if he entered the same number as the prevoius user entered which it already exist in DB ,, it shows u an Error Massage instead of Crash ..

Any Help Could be Appreciated ..

Thanks in Advance,
Mahmood Haji
 
If you put a unique constraint on that column, attempting to insert a duplicate value will cause an exception to be thrown. You can catch that exception and display a MessageBox to the user.
 
Thank bro 4 ur comment ..

im looking for a code of it if u can .. coz i haven't work on it before ,, and it's required in my System which im implementing it in my work ..

..
 
i been working with SQL Databases before ,, exactly when inserting, deleting, updating & selecting Data .. and also, searching 4 an Specific row and dispaly it in Grid View .. but doesnt worked in such things like Exceptions .. this is something gonna be new for me ..
 
In VB.NET, exception handling is implemented using the Try statement and the associated Catch and Finally statements. You should read about exception handling in general and those statements in particular. Once you've covered the basics, if there's something specific you don't understand, post back here.
 
..

Hi bro ,, i just went through Exception Handling but found nothing to do with my Issue ..!!

i need a logic which can be provided to do checking in the DataBase ,, if the specific value been taken ,, i use an label or a MsgBox to Display an Error ..

..
 
You found lots of information that can help you. You just didn't apply it. Exception handling has nothing to do with Labels or MessageBoxes. If you want to display text in a Label or MessageBox then by all means do so. I would expect that you already know how to do that. Exception handling is all about attempting to execute code and cleaning up gracefully is an exception is thrown rather than crashing. As you should now know from your reading, you can wrap your code in a Try block and, if an exception is thrown, it jumps to the Catch block. If you end up in the Catch block then you know an exception was thrown, i.e. the code didn't work. What do you want to do if the code doesn't work? Display text in a Label or MessageBox.

Don't expect to always find code that you can copy and paste and have your problem fixed. programming is about taking the bits and pieces you know and putting them together is such a way as to solve your specific problem. This is one of those cases. You know how to handle an exception and you know how to display a message to a user. Put the two together and you can display a message to the user when an exception is handled.
 
i agree bro with u ,, and my thanks to u 4 ur comment and a shortcut bout how i can handle my problem .. and im Here to know the logic exactly before i go coding .. The concept of this process is still not clear that much 4 me ,, i mean i don't know How i can solve it ,, either by doing what u mentioned ,, or by using LINQ process using DataContexts or either by the Following Code:

Dim strDb, Str As String

Dim cmd As New SqlCommand
'Dim dr As New SqlDataReader
Dim con As SqlConnection = New SqlConnection("Data Source = mahmood-nb\sqlexpress;Database = Tracking_of_Invoices;Uid = sa;Pwd = sa;")
con.Open()
Str = "SELECT * FROM Details WHERE Sales_Invoice = '" & txtSIO.Text & "'"
cmd = New SqlCommand(Str, con)
Dim dr As SqlDataReader = cmd.ExecuteReader()
If Not dr.HasRows Then
Str = "insert into Details value('" & txtSIO.Text & "')"
cmd = New SqlCommand(Str, con)
cmd.ExecuteNonQuery()
Else
Label4.Text = "Record exists"
End If
con.Close()
-- WHich it still has a problem ..

so, i need the PATH not the code ,, to bulid my vision on it .. i need to learn something for myself .. :)
 
Back
Top