Click here to Skip to main content
15,921,941 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a table name flight_info(flight_id, flight_no, depart_from, destination, depart_time, arrive_time).
Now I want to retrieve only flight_id on the basis of column depart_from and destination.
I wrote the SQL as following:

SQL
string str = "SELECT fligth_id FROM flight_info WHERE  depart_from = @depart AND destination = @destination";


While running, it's showing an error pointing @depart and @destination.
Can you help me, How can I specify those scalar variable.?

I tried it

C#
SqlDataReader myReader;

string depart = myReader["depart_from"].ToString();
string destination = myReader["destination"].ToString();


it is not working. Help needed.
Posted

Hi Sopner Feriwala,

Looking at your code,
C#
string str = "SELECT fligth_id FROM flight_info WHERE  depart_from = @depart AND destination = @destination";


it specifies that @depart and @destination are the parameters. You can either add paramters to the sql command or just pass the values into the string like below

C#
string str = "SELECT fligth_id FROM flight_info WHERE  depart_from = " + strDepart + " AND destination = " + strDestination";


Here strDestination and strDepart are two string variables and assign the values before passing into the query.

Rather than passing the values directly into the query, I suggest you to take a look at passing the values as parameters and using stored procedures in order to avoid security issues. Assuming you are using one of the SQL Server 2005/08/12 versions, follow the below links for more details.

http://stackoverflow.com/questions/3806229/how-to-pass-value-to-a-stored-procedure-using-c-sharp[^]

http://net-informations.com/csprj/data-providers/cs-procedure-parameter.htm[^]

Thank you,
Vamsi
 
Share this answer
 
Take a look at following code, see if it works:


C#
// Find the Employee and display it in the List Box
        private void btnFindFlightId_Click(object sender, System.EventArgs e)
        {
            SqlDataReader rdr = null;
            SqlConnection con = null;
            SqlCommand cmd = null;
            int flightId = 0;
            try
            {
                // Open connection to the database
                string ConnectionString = "server=YourServerName;uid=UserName;"+
                    "pwd=Password; database=YourDatabaseName"; // Use your database connection here.
                con = new SqlConnection(ConnectionString);
                con.Open();
                // Set up a command with the given query and associate
                // this with the current connection.
                string CommandText = "SELECT Flight_Id" +
                                     "  FROM Flight_Info" +
                                     " WHERE (depart_from = @depart)" +
                                     " AND destination = @destination";
                cmd = new SqlCommand(CommandText);
                cmd.Connection = con;
                // Add LastName to the above defined paramter @Find
                cmd.Parameters.Add(
                    new SqlParameter(
                    "@depart", // The name of the parameter to map
                    System.Data.SqlDbType.VarChar, // SqlDbType values
                    20, // The width of the parameter
                    "Depart"));  // The name of the source column

                cmd.Parameters.Add(
                    new SqlParameter(
                    "@destination", // The name of the parameter to map
                    System.Data.SqlDbType.VarChar, 
                    20, // The width of the parameter
                    "Destination"));  
                // Fill the parameter with the value retrieved
                // from the text field
                cmd.Parameters["@depart"].Value = txtDepart.Text; // This is the input field. 
                cmd.Parameters["@destination"].Value = txtDestination.Text; // This is the input field. 
                // Execute the query
                rdr = cmd.ExecuteReader();
                while(rdr.Read())
                {
                    flightId = Convert.ToInt32(rdr[Flight_Id]);// you can write your own way of using flight id further 
                }
            }
            catch(Exception ex)
            {
                // error message
                throw(ex.Message);
            }
            finally
            {
                // Close data reader object and database connection
                if (rdr != null)
                    rdr.Close();
                if (con.State == ConnectionState.Open)
                    con.Close();
            }
        }
    }
 
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