how to compare a text box value with a table (store in sql server)
i am working on a web site. i need to validate a text box value with a Column ( in table A, using sql server) .
well, the situation is. When user enter in textbox1 = 45 , before user go to textbox2开发者_如何学Python , textbox1 check in database if 45 is there. If not give error .
thanks
Dim Conn as new sqlconnection(..add your sql connection string here..)
Dim cmd as new sqlcommand("SELECT COUNT(*) FROM [TableA] WHERE [Column]=@Value",Conn)
cmd.parameters.add("@Value",sqldbtype.nvarchar).value = textbox1.text
Conn.open
Dim valueExistsInDB as boolean = cbool(cint(cmd.executescalar())>0)
conn.close
Depending on whether the value exists in the table, valueExistsInDB
will return True/False
Apart from what Curt shows, you will have to consider how to transfer the information; are both textboxes on the same page? Then you might either need some sort of AJAX communication between server (that have the DB connection, I assume) and client (the web browser displaying the page), OR you have to submit the form when leaving textbox1, returning a new page with the textbox1 now containing validated data.
You can connect to a SQL server from the browser, but it's not a good idea. Have a look at How to connect to SQL Server database from JavaScript in the browser? for details.
精彩评论