Click here to Skip to main content
15,881,757 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
I want to validate the textbox using my data table.

i want to validate text box value all ready in data table.

EX-001,002 in my table Any one reEnter the 001 .i want to vaild it??
Posted
Comments
Nandakishore G N 12-Feb-13 1:22am    
block using Stored procedure
Ankur\m/ 12-Feb-13 1:47am    
[moved from direct comment]
breaker-code - 20 mins ago
i Need a how to write a method.

breaker-code - 12 mins ago
Help me plz
[no name] 12-Feb-13 1:24am    
i Need a how to write a method.
Ankur\m/ 12-Feb-13 1:48am    
Use the 'Reply' link against a comment to reply to it. Direct comment doesn't notify the user.
[no name] 12-Feb-13 1:49am    
k .am sry

C#
protected void Button1_Click(object sender, EventArgs e)
    {
         using (SqlConnection con = new SqlConnection("----"))
        {
            con.Open();
            SqlCommand cmd = new SqlCommand("Select * FROM TEST WHERE ID='"+TextBox1.Text+"'", con);
            SqlDataAdapter da = new SqlDataAdapter(cmd);
            DataTable DT = new DataTable();
            da.Fill(DT);
            con.Close();
            if (DT.Rows.Count > 0)
            {
                Response.Write("ALREADY HAVE");
            }
            else
            {
                //YOUR CODE//
            }
            
        }
    }
 
Share this answer
 
v2
Comments
fjdiewornncalwe 12-Feb-13 11:50am    
I would prefer to see the query done with parameters rather than inline concatenation which is a sql injection threat, but it does answer the OP's question. +5.
Avik Ghosh22 12-Feb-13 12:05pm    
thanks ....
hi,

You can check using Lambda expression as below.
C#
if(objdt.AsEnumerable().Where(c => c.Field<string>("Col1") == txtbox1.Text.Trim()).Count() > 0)
{
   //valid text
}
else
{
  //invalid text
}

//Col1 is the column in your datatable.</string>


this will check the existence of the textbox value in datatable, if count > 0 , then that value exists in table else not.

More on lambda expression[^]

hope it helps.
 
Share this answer
 

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