I did my homework prior to asking this questions. Though, none of the results that google showed functioned .
I have a textbox whose input I wish to validate against a list of values which exist in a column of a table (ASP.NET with C# and SQL Server 2014 Express) . Should user enter some other value, than the error must be displayed.
I have done multiple tryouts with CustomValidator control and one when an event on the Textbox (.TextChanged). But I lost something, may be in the details. Could you give me a practical solution and at best, guide towards a useful online resource to study the connection to databases from asp.net (c#)?
I am aware that I did not catch the subject.
答案 0 :(得分:0)
Here is a basic rough version how you could get it done. Can also use a Stored procedure rather than direct..Wasn't sure if you meant after a button was clicked or not, but you can put this in a method and have it return true or false if its valid.
SqlConnection cnn = null;
SqlCommand cmd = null;
SqlDataAdapter sda = null;
DataTable Dt = new Datatable();
cnn = new SqlConnection(strConnectionString);
cmd = new SqlCommand("Select COLUMN FROM WHEREVER WHERE VALUE =@TextboxValue", cnn);
cnn.Open();
cmd.CommandType = CommandType.Text;
cmd.Parameters.Add("@TextboxValue", SqlDbType.VarChar).Value = Textbox.Text;
sda = new SqlDataAdapter(cmd);
sda.Fill(dt);
if (dt.rows.count > 0 )
{
//MATCH FOUND
}