0

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 , textbox1 check in database if 45 is there. If not give error .

thanks

Curtis
  • 101,612
  • 66
  • 270
  • 352
dca
  • 31
  • 1
  • 6

2 Answers2

0
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

Curtis
  • 101,612
  • 66
  • 270
  • 352
  • The ConnectionString property has not been initialized. This Error after i run above code – dca Mar 31 '11 at 14:42
  • This will be to do with whatever youve replaced my '..add your sql connection string here..' with. – Curtis Mar 31 '11 at 14:44
  • Dim Conn As New SqlConnection(ConnString) Dim cmd As New SqlCommand("SELECT COUNT(*) FROM [Districts_Functions] WHERE [PayID]=@Value", Conn) cmd.Parameters.Add("@Value", SqlDbType.NVarChar).Value = tbpaycode.Text Conn.open() Dim valueExistsInDB As Boolean = CBool(CInt(cmd.ExecuteScalar()) > 0) Conn.Close() If valueExistsInDB = False Then MsgBox("Enter a Valid Value") End If – dca Mar 31 '11 at 14:50
  • Private Shared ReadOnly ConnString As String – dca Mar 31 '11 at 14:51
  • Does ConnString have a value? Remember, this needs to be your connection string. – Curtis Mar 31 '11 at 15:52
0

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.

Community
  • 1
  • 1
JenEriC
  • 758
  • 7
  • 10
  • yes its kinda like that as you describe. But submit Button at the end of form take all the data into data base. And yes both text boxes are in the same page – dca Mar 31 '11 at 14:32
  • The submit button is not the only way to submit a form. :-) You can easily do it by onChange events on textbox1, for example, and you could easily set hidden form fields to represent the current state, thus knowing that textbox1 isn't evaluated yet. But a complete submit and re-send of the page probably takes much longer than an AJAX call. However, I don't know how to do AJAX in a vb.net environment. – JenEriC Mar 31 '11 at 14:39
  • i am using onChange events, but dont know how to get data from database to compare or AJAx – dca Mar 31 '11 at 14:43