Click here to Skip to main content
14,875,778 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Database as follows;

Student id

1
2
3
4
5
6

Table name Student.

My design as follows;

Student id Textbox1


In run mode when i enter the Student id, i want to validate whether that student id in the Student table or not.

if user wrongly enter the student id in Textbox1, shows the message Enter Correct student id.

how can i validate.

Please help me.

Regards,
Narasiman P.
Posted
Comments
a.anvesh 30-Apr-13 4:52am
   
Can you please tell me i.e in windows application or web application
Zoltán Zörgő 30-Apr-13 4:58am
   
ID in a textbox?!?!?! Never!
a.anvesh 30-Apr-13 5:05am
   
You can use textbox leave event in that you have to write database connection and check with student table.

In the textbox validating event query the database (unless you already have the data in memory) for an entry with the given ID. If it returns nothing then there is no entry. If it returns something then it is valid. And you already have the students info in memory if you need it.
   
Try:
C#
using (SqlConnection con = new SqlConnection(strConnect))
    {
    con.Open();
    using (SqlCommand cmd = new SqlCommand("SELECT COUNT(iD) FROM myTable WHERE id=@ID", con))
        {
        cmd.Parameters.AddWithValue("@ID", textbox1.Text);
        int count - cmd.ExecuteScalar();
        if (count != 0)
            {
            // It exists.
            }
        else
            {
            // It doesn't exist
            }
        }
    }
   
try This



Create An Function which have bool return Type

public bool Validate_Id(int Id) //id=txtid.text
{
Open The Connection
string StrSQL = "select count(*) from Student where "+Id+"";
SqlCommand cmd = new SqlCommand(StrSQL, cn);

SqlDataReader dr;
dr = cmd.ExecuteReader();
dr.Read();
int i = 0;
if (dr.HasRows)
{
i = Convert.ToInt32(dr.GetValue(0).ToString());
}


if (i > 0)
{
return true;
}
else
{
return false;
}
}

if it return true then id Exist


Hope So It Will Help You
   
v3

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900