Reputation:
txtBox2.Enabled = false;
//made by francisco juliao
Convert.ToInt32(txtBox1.Text);
int Ticket = Convert.ToInt32(txtBox1.Text);
SqlConnection mySC = new SqlConnection();
SqlCommand com = new SqlCommand();
mySC.ConnectionString = ("Data Source=localhost;Initial Catalog=RCA;Integrated Security=True");
mySC.Open();
com.Connection = mySC;
com.CommandText = (@"SELECT Ticketid * FROM RCA)
VALUES ('" + txtBox1.Text + "','" + output+ "');");
mySC.Close();
if (Ticket = //SQL ticketID check here.
txtBox2.Enabled = true;
MessageBox.Show("Valid ID! Input email now.");
label1.Text = "Enter your email now.";
}
else
{
MessageBox.Show("Incorrect!");
}
So I want my C# app to check if the ticketID(txtBox1.Text)
that was typed by the user is in the SQL server. I have no idea how I would go about doing this. If the input is correct the label changes and the textbox2
gets enabled. If the email is not found in the sql database, a messagebox is shown with the text "incorrect!". Any help would be appreciated!
Upvotes: 1
Views: 471
Reputation: 14064
You should change the code on the Lost Focus event of the textbox1 i.e. your TicketID textbox.
That would look like this
private void txtBox1_LostFocus(object sender, RoutedEventArgs e)
{
SqlConnection mySC = new SqlConnection();
SqlDataReader reader;
mySC.ConnectionString = ("Data Source=localhost;Initial Catalog=RCA;Integrated Security=True");
mySC.Open();
string sql = @"SELECT * FROM RCA Where Ticketid = @tktid;";
using(var command = new SqlCommand(sql, mySC))
{
command.Parameters.Add("@tktid", txtBox1.Text);
reader = command.ExecuteReader();
}
if(reader.HasRows)
{
//Here you change the Label with LabelName I have used string to show.
string Lbllabel = "Change";
txtEmail.IsEnabled = true;
}
mySC.Close();
}
Now to check the email you should use the same thing on TxtEmail_LostFocus
private void txtEmail_LostFocus(object sender, RoutedEventArgs e)
{
SqlConnection mySC = new SqlConnection();
SqlDataReader reader;
mySC.ConnectionString = ("Data Source=localhost;Initial Catalog=RCA;Integrated Security=True");
mySC.Open();
string sql = @"SELECT * FROM RCA Where EmailID = @emailid;";
using(var command = new SqlCommand(sql, mySC))
{
command.Parameters.Add("@emailid", txtEmail.Text);
reader = command.ExecuteReader();
}
if(reader.HasRows)
{
//Email Exsist in the dataBase.
}
else
{
MessageBox("Incorrect!!");
}
mySC.Close();
}
Upvotes: 2
Reputation: 29
At first you are Converting the text to int
Convert.ToInt32(txtBox1.Text);
But you don't do anything with the result!? like this for example:
int a = Convert.ToInt32(txtBox1.Text);
using Convert.toInt32() doesn't change anything in the textbox. It just takes the string of the text and returns it converted to int.
If you want to validate the entry by checking if it is a number, you could do this:
try{
int a = Convert.ToInt32(txtBox1.Text);
}
catch{
// error, txtBox1.Text could not be converted to int (it contains letters)
}
To ckeck if ticketID is in table make an sql statement like this:
cmd.Commandtext = "SELECT Count(*) FROM RCA WHERE ticketID = '" + txtBox.text + "'";
After that you execute it like a query and safe the result in a reader
SqlDataReader reader = cmd.ExecuteReader();
than you read the results from the reader and see if there was a match
...or you could just do it like Mohit Shrivastava says. Very nice and shiny! :D
Upvotes: 0