Jay V

Jay V

  • NA
  • 1
  • 0

C# database

May 7 2010 4:28 PM

this is our code it is giving us an error when we try changing to the next passenger
it gives us Invalid attempt to read when no data is present.
someone please help
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace Flight
{
    public partial class passengerdata : Form
    {
        SqlConnection sql;
        SqlCommand query;
        SqlDataReader results;
        int pid, pflight;

        SqlDataAdapter read;
        DataTable table;
        SqlCommandBuilder cmd;

        public passengerdata()
        {
            InitializeComponent();
        }

        private void passengerdata_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'flinflondbDataSet1.passenger' table. You can move, or remove it, as needed.
            sql = new SqlConnection("Data Source=.\\SQLEXPRESS;" +
                                       "AttachDbFilename=" + address.Address + ";" +
                                       "Integrated Security=True; Connect Timeout=30;" +
                                       "User Instance=True;");
            sql.Open();

            read = new SqlDataAdapter("SELECT * FROM passenger", sql);
            cmd = new SqlCommandBuilder(read);
            table = new DataTable();
            table.Locale = System.Globalization.CultureInfo.InvariantCulture;
            read.Fill(table);
            passengerBindingSource.DataSource = table;

            this.passengerTableAdapter.Fill(this.flinflondbDataSet1.passenger);
            cmbPassenger.SelectedIndex = 0;
            cmbPassenger_SelectedIndexChanged(sender, e);
        }

        private void btn_closepassengerdata_Click(object sender, EventArgs e)
        {
            this.flinflondbDataSet1.AcceptChanges();
            this.Close();
        }

        private void btn_data_Click(object sender, EventArgs e)
        {
           
        }

        private void cmbPassenger_SelectedIndexChanged(object sender, EventArgs e)
        {
            try
            {
                DataRowView drv;
                drv = (DataRowView)cmbPassenger.SelectedItem;
                object[] resultsArray = drv.Row.ItemArray;

                lblNameData.Text = Convert.ToString(resultsArray[1])
                    + ' ' + Convert.ToString(resultsArray[2]);
                lblAddressData.Text = Convert.ToString(resultsArray[3]);
                lblCityData.Text = Convert.ToString(resultsArray[4]);
                lblStateData.Text = Convert.ToString(resultsArray[5]);
                lblZipData.Text = Convert.ToString(resultsArray[6]);
                lblAgencyData.Text = Convert.ToString(resultsArray[8]);

                if (Convert.ToBoolean(resultsArray[9]))
                {
                    pid = Convert.ToInt32(resultsArray[0]);
                    query = new SqlCommand("SELECT * FROM booking WHERE " +
                        "p_id = " + pid + ";", sql);
                    results = query.ExecuteReader();
                    results.Read();
                    pflight = Convert.ToInt32(results["p_flight"]);
                    results.Close();
                    query.Cancel();
                    query = new SqlCommand("SELECT * FROM flight WHERE " +
                        "f_id = " + pflight + ";", sql);
                    results = query.ExecuteReader();
                    results.Read();
                    lblBookedData.Text = "Passenger booked on: " +
                        Convert.ToString(results["f_name"]);
                }
                else
                {
                    lblBookedData.Text = "No flights booked.";
                }
                results.Close();

            }
            catch (NullReferenceException ez)
            {
            }
        }
    }
}

Answers (1)