By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,344 Members | 1,114 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,344 IT Pros & Developers. It's quick & easy.

How to require the user to fill all fields?

P: 21
Expand|Select|Wrap|Line Numbers
  1. Imports MySql.Data.MySqlClient
  2. Partial Class _Default
  3.     Inherits System.Web.UI.Page
  4.     Dim ServerString As String = "Server=localhost;User Id=root;Password=1234;Database=AS"
  5.     Dim SQLConnection As MySqlConnection = New MySqlConnection
  6.  
  7.     Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
  8.  
  9.  
  10.         SQLConnection.ConnectionString = ServerString
  11.  
  12.         Try
  13.             If SQLConnection.State = Data.ConnectionState.Closed Then
  14.                 SQLConnection.Open()
  15.                 MsgBox("Succesfully connected to MySQL database!")
  16.             Else
  17.                 SQLConnection.Close()
  18.                 MsgBox("Connection is closed!")
  19.  
  20.             End If
  21.         Catch ex As Exception
  22.             MsgBox(ex.ToString)
  23.  
  24.         End Try
  25.     End Sub
  26.  
  27.     Protected Sub cmdSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles cmdSave.Click
  28.  
  29.         If txtName.Text = "" Then
  30.             MsgBox("Please fill in all boxes")
  31.         End If
  32.         If txtPassword.Text = "" Then
  33.             MsgBox("Please fill in all boxes")
  34.         End If
  35.         If txtFname.Text = "" Then
  36.             MsgBox("Please fill in all boxes")
  37.         End If
  38.         If txtMname.Text = "" Then
  39.             MsgBox("Please fill in all boxes")
  40.         End If
  41.         If txtLname.Text = "" Then
  42.             MsgBox("Please fill in all boxes")
  43.         End If
  44.         If txtCitizenship.Text = "" Then
  45.             MsgBox("Please fill in all boxes")
  46.         End If
  47.         If txtCountry.Text = "" Then
  48.             MsgBox("Please fill in all boxes")
  49.         End If
  50.         If txtOccupation.Text = "" Then
  51.             MsgBox("Please fill in all boxes")
  52.         End If
  53.         If txtSanswer.Text = "" Then
  54.             MsgBox("Please fill in all boxes")
  55.         End If
  56.  
  57.         Dim SQLStatement As String = "INSERT INTO tbluserinfo(userName,userPassword,userFname,userMname,userLname,userBmonth,userBday,userByear,userGender,userCstatus,userCitizenship,userCountry,userOccupation,userSquestion,userSanswer) VALUES('" & txtName.Text & "', '" & txtPassword.Text & "', '" & txtFname.Text & "', '" & txtMname.Text & "', '" & txtLname.Text & "','" & cmbBmonth.Text & "','" & cmbBday.Text & "','" & cmbByear.Text & "','" & cmbGender.Text & "','" & cmbCstatus.Text & "','" & txtCitizenship.Text & "','" & txtCountry.Text & "','" & txtOccupation.Text & "','" & cmbSquestion.Text & "','" & txtSanswer.Text & "' )"
  58.         SaveNames(SQLStatement)
  59.  
  60.  
  61.     End Sub
  62.  
  63.     Public Sub SaveNames(ByRef SQLStatement As String)
  64.         Dim cmd As MySqlCommand = New MySqlCommand
  65.  
  66.         With cmd
  67.             .CommandText = SQLStatement
  68.             .CommandType = Data.CommandType.Text
  69.             .Connection = SQLConnection
  70.             .ExecuteNonQuery()
  71.  
  72.         End With
  73.  
  74.         SQLConnection.Close()
  75.         MsgBox("Succesfully Added!")
  76.         SQLConnection.Dispose()
  77.     End Sub
  78.  
  79.  
  80. End Class



And I want to know where to put the code on checking empty fields before it can be save on database
Aug 17 '12 #1

✓ answered by ariful alam

your code shows the checking block cmdSave_Click is for cmdSave button. than what you need?

moreover in visual studio.net you need not write code for checking field is empty or not. you can use Required Field Regular Expression to check the fields empty or not.

Share this Question
Share on Google+
4 Replies


ariful alam
100+
P: 185
your code shows the checking block cmdSave_Click is for cmdSave button. than what you need?

moreover in visual studio.net you need not write code for checking field is empty or not. you can use Required Field Regular Expression to check the fields empty or not.
Aug 17 '12 #2

P: 21
thankyou ariful alam=)
Aug 17 '12 #3

PsychoCoder
Expert Mod 100+
P: 465
For the record you're code is prime for SQL Injection attack. I suggest you use parameterized queries when working with data.

http://bytes.com/topic/net/insights/...trized-queries
Aug 18 '12 #4

P: 21
I'm using MySql as my database, sorry I'm new to this. I'm working for a school thesis
Aug 28 '12 #5

Post your reply

Sign in to post your reply or Sign up for a free account.