0

enter image description here

i am having this error in the image above whenever i want to book a flight in my airline reservation system project there is an exception error in my code this error message was popping up

IndexOutOfRangeException unhandles by user code

...any ideas on how i can go about it my code is written in asp.net using c sharp??

this is code

public partial class TICKET_BOOKING : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }
    int a;
    String constring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    public int Autonumber()
    {
        SqlConnection con = new SqlConnection(constring);
        SqlCommand cmd = new SqlCommand();
        cmd.CommandType = CommandType.Text;
        cmd.Connection = con;
        con.Open();
        cmd.CommandText = "select pid from pid";
        SqlDataAdapter da = new SqlDataAdapter();
        DataSet ds = new DataSet();
        DataTable dt = new DataTable();
        da.SelectCommand = cmd;
        da.Fill(dt);
        a = Convert.ToInt32(dt.Rows[0][0].ToString()) + 1; //this is where its occuring
        con.Close();
        return a;
    }

2 Answers 2

2

Try this

    int a;
    String constring = ConfigurationManager.ConnectionStrings["conn"].ConnectionString.ToString();
    public int Autonumber()
    {
        using (SqlConnection con = new SqlConnection(constring))
        {
            SqlCommand cmd = new SqlCommand
                                 {
                                     CommandType = CommandType.Text,
                                     Connection = con,
                                     CommandText = "select pid from pid",
                                 };
            con.Open();
            // cmd.Connection.Open();
            using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
            {
                while (reader.Read())
                {
                    if (!reader.IsDBNull(0))
                        a = reader.GetInt32(0);
                }
            }
            return a;
        }
    }
Sign up to request clarification or add additional context in comments.

4 Comments

i tried it again there another error ExecuteReader: Connection property has not been initialized.An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.Exception Details: System.InvalidOperationException: ExecuteReader: Connection property has not been initialized.Line 31: using (var reader = cmd.ExecuteReader(CommandBehavior.CloseConnection))
Edited with Connection = con Please try now.
now its telling me SqlException was unhandled by the use. Violation of PRIMARY KEY constraint 'PK_Plist'. Cannot insert duplicate key in object 'dbo.Plist'. The statement has been terminated.
This error PRIMARY KEY violation occurs when your trying to insert the same value which already exists (This is knowmn to us). But you are not inserting any record infact you are querying 'select pid from pid'. are you using same query? It is working fine for me.
0

You don't have a records in your datable so next

dt.Rows[0][0]

fails. Please check if table have a records before acceding them. Something like that:

if(dt.Rows.Count > 0) 
{ 
   a = Convert.ToInt32(dt.Rows[0][0].ToString()) + 1; 
}

1 Comment

i tried this it seems to work though another error is popping up Violation of PRIMARY KEY constraint 'PK_Plist'. Cannot insert duplicate key in object 'dbo.Plist'.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.