Reputation: 1184
I have a WinForm with TextBox inside.
When I activate TextBox.Leave
event what I need is to check that entry TextBox.text
in my DataSet
(Column, not Row) is that entry existing, if not I just have to get a MsgBox("The text you entered already exists, use another one")
and it should not be allowed to submit that into DataBase.
Upvotes: 1
Views: 1230
Reputation: 2992
Since you mention DataSet Try this (Dataset is not a table but a collection of table/s)
Dim Tot() as DataRow = yourDataset.Tables("tableName").Select("theColName= '" + TextBox.Text.Trim() + "'")
or
Dim Tot() as DataRow = yourDataset.Tables(IndexNumberOfTable).Select("theColName= '" + TextBox.Text.Trim() + "'")
Upvotes: 0
Reputation: 393
try this code, I reproduce it for you it is working fine... Double Check your values what you enter in textbox...
Public Class Form1
Dim table As DataTable
Dim ds As DataSet
Private Sub TextBox1_Leave(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.Leave
Dim result() As DataRow = ds.Tables(0).Select("Name = '" + TextBox1.Text.Trim() + "'")
If (result.Length > 0) Then
MsgBox("Value Exist")
Else
'do your calculation
End If
End Sub
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
table = New DataTable("Players")
' Add two columns.
table.Columns.Add(New DataColumn("Name", GetType(String)))
table.Columns.Add(New DataColumn("age", GetType(String)))
ds.Tables(0).Select()
table.Rows.Add("Magesh", "25")
table.Rows.Add("flook", "22")
ds.Tables.Add(table);
End Sub
End Class
Upvotes: 1
Reputation: 195
you can write the sql query as select cols from tablename where col='yourtextboxvalue' execute a datareader using commandObject.executeReader and execute dataReader.Read() method in try block. if value doesn't exist it will go to catch block and show the messagebox there. similar thing to check for username
SqlCommand command = new SqlCommand("Select Password from tblUser where UserName=@username", connection);
command.Parameters.AddWithValue("@username", txtUsername.Text); //add parameter to the query
if (dataReader.Read()) //if any record is available
{
dbPassword = dataReader["Password"].ToString(); //get the password for entered username
if (dbPassword != "" && dbPassword.Equals(password))
{
}
else
MessageBox.Show("Invalid Password","Invalid input");
connection.Close(); //close the connection
}
else
MessageBox.Show("Username not found", "Invalid input"); //message will be shown if no record found for the entered user ie.username doesn't exist
Upvotes: 0
Reputation: 393
Put this code inside textbox1 leave event
Dim result() As DataRow = DataTable.Select("ColumnName = '" + TextBox1.Text.Trim() + "'")
and if result.length > 0 then don't save it....
Upvotes: 0
Reputation: 11
From database perspective, the easiest way to prevent duplicate entries into a column is if you set a unique constraint on the given column:
CREATE TABLE table1
(
Id int NOT NULL,
Name varchar(255) NOT NULL,
CONSTRAINT uc_Name UNIQUE (Name)
)
Upvotes: 0