Reputation: 131
ok so i have a text box and a button where i will enter a number and press the button. I then want a message box to display if the value has been found or not.
I put all my functions in a model class then call them for the GUI part. heres what i tried so the sql function (ticket is the name of the table and ID is the value im trying to find):
public void rebateslip(int ticketID)
{
SqlCommand myCommand = new SqlCommand("SELECT ID FROM ticket WHERE ID = @ticketID", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(myCommand);
myCommand.Parameters.AddWithValue("@ID", ticketID);
}
and than for the button event handler i have this:
private void buttonPrintRebateSlip_Click(object sender, EventArgs e)
{
if (textBoxRebateSlip = model.rebateslip(ticketID)
{
MessageBox.Show("Found ticket");
}
else
{
MessageBox.Show("Ticket not in database");
}
}
but it says ticketID does not exist
Upvotes: 0
Views: 10588
Reputation: 3105
you should change your model.rebateslip
method so that it return a bool
, and execute your command, then it should look more or less like this: (don't have the SqlCommand methods in mind, nor the reader's ones, but according to my memory it looks like this)
public bool rebateslip(int ticketID)
{
SqlCommand myCommand = new SqlCommand("SELECT ID FROM ticket WHERE ID = @ticketID", con);
SqlDataAdapter sqlDa = new SqlDataAdapter(myCommand);
myCommand.Parameters.AddWithValue("@ticketID", ticketID);
var reader = myCommand.Execute();
return reader.HasRows;
}
then you should do something like @Ezekiel said in his answer:
int id;
if (!int.TryParse(textBoxRebateSlip.Text, out id)) return;
if (model.rebateslip(id))
...
Upvotes: 0
Reputation: 434
The If statement is not correct, you are missing a closing paren and the comparison is not valid either. Use == for comparison. Furthermore, ticketID variable is not initialized anywhere. From your description it sounds like you are getting it from the textbox? If so it should be something like this:
int id = Convert.ToInt32(textBoxRebateSlip .Text);
if (model.rebateslip(ticketID) == id)
...
Another thing I noticed, you are not executing the sql command anywhere?
Also, the rebateslip method has return type void, so it will not work.
With all these errors, this code would not even compile.
Upvotes: 0
Reputation: 75316
Your parameter is named ticketID
, not ID
So, you should change to:
myCommand.Parameters.AddWithValue("@ticketID", ticketID);
Upvotes: 1