Take a look at following code, see if it works:
private void btnFindFlightId_Click(object sender, System.EventArgs e)
{
SqlDataReader rdr = null;
SqlConnection con = null;
SqlCommand cmd = null;
int flightId = 0;
try
{
string ConnectionString = "server=YourServerName;uid=UserName;"+
"pwd=Password; database=YourDatabaseName";
con = new SqlConnection(ConnectionString);
con.Open();
string CommandText = "SELECT Flight_Id" +
" FROM Flight_Info" +
" WHERE (depart_from = @depart)" +
" AND destination = @destination";
cmd = new SqlCommand(CommandText);
cmd.Connection = con;
cmd.Parameters.Add(
new SqlParameter(
"@depart",
System.Data.SqlDbType.VarChar,
20,
"Depart"));
cmd.Parameters.Add(
new SqlParameter(
"@destination",
System.Data.SqlDbType.VarChar,
20,
"Destination"));
cmd.Parameters["@depart"].Value = txtDepart.Text;
cmd.Parameters["@destination"].Value = txtDestination.Text;
rdr = cmd.ExecuteReader();
while(rdr.Read())
{
flightId = Convert.ToInt32(rdr[Flight_Id]);
}
}
catch(Exception ex)
{
throw(ex.Message);
}
finally
{
if (rdr != null)
rdr.Close();
if (con.State == ConnectionState.Open)
con.Close();
}
}
}