locked
System.IndexOutOfRangeException was unhandled RRS feed

  • 問題

  •  

    why I am getting this eeror for this line?

    txtArea.Text = sql_reader["address_area"].ToString();

     

    and when I remark it, it shows the same error for the next line:

    txtBox.Text = sql_reader["address_po_box"].ToString();

     

    but all lines above these two lines are ok? also when I move one of the lines to the top of the code I get no errors!!

     

    here is the error details:

     

    System.IndexOutOfRangeException was unhandled
      Message="address_area"
      Source="System.Data"
      StackTrace:
           at System.Data.ProviderBase.FieldNameLookup.GetOrdinal(String fieldName)
           at System.Data.SqlClient.SqlDataReader.GetOrdinal(String name)
           at System.Data.SqlClient.SqlDataReader.get_Item(String name)
           at shefa.frmPatientDetails.get_person() in C:\Users\Jassim Rahma\Documents\Visual Studio 2005\Projects\shefa\shefa\patient_details_form.cs:line 200
           at shefa.frmPatientDetails.frmPatientDetails_Shown(Object sender, EventArgs e) in C:\Users\Jassim Rahma\Documents\Visual Studio 2005\Projects\shefa\shefa\patient_details_form.cs:line 450
           at System.Windows.Forms.Form.OnShown(EventArgs e)
           at System.Windows.Forms.Form.CallShownEvent()
           at System.Windows.Forms.Control.InvokeMarshaledCallbackDo(ThreadMethodEntry tme)
           at System.Windows.Forms.Control.InvokeMarshaledCallbackHelper(Object obj)
           at System.Threading.ExecutionContext.runTryCode(Object userData)
           at System.Runtime.CompilerServices.RuntimeHelpers.ExecuteCodeWithGuaranteedCleanup(TryCode code, CleanupCode backoutCode, Object userData)
           at System.Threading.ExecutionContext.RunInternal(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Windows.Forms.Control.InvokeMarshaledCallback(ThreadMethodEntry tme)
           at System.Windows.Forms.Control.InvokeMarshaledCallbacks()
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ContainerControl.WndProc(Message& m)
           at System.Windows.Forms.Form.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Form.ShowDialog(IWin32Window owner)
           at System.Windows.Forms.Form.ShowDialog()
           at shefa.frmPatientDashboard.toolPatientDetails_Click(Object sender, EventArgs e) in C:\Users\Jassim Rahma\Documents\Visual Studio 2005\Projects\shefa\shefa\patient_dashboard.cs:line 78
           at System.Windows.Forms.ToolStripItem.RaiseEvent(Object key, EventArgs e)
           at System.Windows.Forms.ToolStripButton.OnClick(EventArgs e)
           at System.Windows.Forms.ToolStripItem.HandleClick(EventArgs e)
           at System.Windows.Forms.ToolStripItem.HandleMouseUp(MouseEventArgs e)
           at System.Windows.Forms.ToolStripItem.FireEventInteractive(EventArgs e, ToolStripItemEventType met)
           at System.Windows.Forms.ToolStripItem.FireEvent(EventArgs e, ToolStripItemEventType met)
           at System.Windows.Forms.ToolStrip.OnMouseUp(MouseEventArgs mea)
           at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
           at System.Windows.Forms.Control.WndProc(Message& m)
           at System.Windows.Forms.ScrollableControl.WndProc(Message& m)
           at System.Windows.Forms.ToolStrip.WndProc(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.OnMessage(Message& m)
           at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m)
           at System.Windows.Forms.NativeWindow.DebuggableCallback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)
           at System.Windows.Forms.UnsafeNativeMethods.DispatchMessageW(MSG& msg)
           at System.Windows.Forms.Application.ComponentManager.System.Windows.Forms.UnsafeNativeMethods.IMsoComponentManager.FPushMessageLoop(Int32 dwComponentID, Int32 reason, Int32 pvLoopData)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoopInner(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.ThreadContext.RunMessageLoop(Int32 reason, ApplicationContext context)
           at System.Windows.Forms.Application.Run(Form mainForm)
           at shefa.Program.Main() in C:\Users\Jassim Rahma\Documents\Visual Studio 2005\Projects\shefa\shefa\Program.cs:line 17
           at System.AppDomain.nExecuteAssembly(Assembly assembly, String[] args)
           at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence assemblySecurity, String[] args)
           at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
           at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
           at System.Threading.ExecutionContext.Run(ExecutionContext executionContext, ContextCallback callback, Object state)
           at System.Threading.ThreadHelper.ThreadStart()

    2006年11月10日 下午 11:16

所有回覆

  • can you show the entire code for that snippet? It could be that the column name does not exist but I would have thought perhaps a different exception if that was the case
    2006年11月10日 下午 11:19
  • the column name dose exists because as i said, when i move the same line to the top of the code it works fine just like all other lines on the beginning of the code.. here is the code with remarks in BLUE.

     

    sql_connection = new SqlConnection("Data Source=.\\SQLEXPRESS;initial catalog=shefa;integrated security=true");

    sql_connection.Open();

    sql_command = new SqlCommand("sp_get_person_by_file", sql_connection);

    sql_command.CommandType = CommandType.StoredProcedure;

    sql_command.Parameters.Add("@file_no", SqlDbType.VarChar).Value = file_no;

    sql_reader = sql_command.ExecuteReader();

    if (sql_reader.Read())

    {

    cboIDType.Text = sql_reader["id_type"].ToString();

    txtIDNumber.Text = sql_reader["id_number"].ToString();

    dateBirth.Value = Convert.ToDateTime(sql_reader["date_of_birth"]);

    txtFullName.Text = sql_reader["person_name"].ToString();

    cboNationality.Text = sql_reader["nationality"].ToString();

    cboGender.Text = sql_reader["gender"].ToString();

    cboMaritalStatus.Text = sql_reader["marital_status"].ToString();

    cboBloodGroup.Text = sql_reader["blood_group"].ToString();

    if (sql_reader["person_is_vip"].ToString() == "Y") chkVIP.Checked = true; else chkVIP.Checked = false;

    cboCountry.Text = sql_reader["address_country"].ToString();

    if (sql_reader["address_country"].ToString() == "BAHRAIN")

    {

    txtArea.Text = sql_reader["address_area"].ToString(); THIS LINE WORKS FINE

    txtFlat.Text = sql_reader["address_flat"].ToString();

    txtBuilding.Text = sql_reader["address_building"].ToString();

    txtRoad.Text = sql_reader["address_road"].ToString();

    txtAvenue.Text = sql_reader["address_avenue"].ToString();

    txtBlock.Text = sql_reader["address_block"].ToString();

    txtArea.Text = sql_reader["address_area"].ToString(); THIS LINE GIVES AN ERROR

    txtBox.Text = sql_reader["address_po_box"].ToString(); IF I COMMENT THE ABOVE LINE THEN THIS LINE GIVES AN ERROR.

    txtGovernorate.Text = sql_reader["address_governorate"].ToString();

    }

    else

    {

    txtAddressOutside.Text = sql_reader["address_outside_address"].ToString();

    }

    txtMobile.Text = sql_reader["contact_moblle"].ToString();

    txtResidenceTel.Text = sql_reader["contact_home_tel"].ToString();

    txtFax.Text = sql_reader["contact_home_fax"].ToString();

    txtEmail.Text = sql_reader["email_address"].ToString();

    cboProfession.Text = sql_reader["profession"].ToString();

    cboCompany.Text = sql_reader["company"].ToString();

    txtOfficeTelephone.Text = sql_reader["contact_office_tel"].ToString();

    txtOfficeDirectLine.Text = sql_reader["contact_ext_direct"].ToString();

    txtOfficeFax.Text = sql_reader["contact_office_fax"].ToString();

    // save data to variables;

    id_type = sql_reader["id_type"].ToString();

    id_number = sql_reader["id_number"].ToString();

    date_of_birth = Convert.ToDateTime(sql_reader["date_of_birth"]);

    person_name = sql_reader["person_name"].ToString();

    nationality = sql_reader["nationality"].ToString();

    gender = sql_reader["gender"].ToString();

    marital_status = sql_reader["marital_status"].ToString();

    blood_group = sql_reader["blood_group"].ToString();

    if (sql_reader["person_is_vip"].ToString() == "Y") person_is_vip = true; else person_is_vip = false;

    address_country = sql_reader["address_country"].ToString();

    address_flat = sql_reader["address_flat"].ToString();

    address_building = sql_reader["address_building"].ToString();

    address_road = sql_reader["address_road"].ToString();

    address_avenue = sql_reader["address_avenue"].ToString();

    address_block = sql_reader["address_block"].ToString();

    address_area = sql_reader["address_area"].ToString();

    address_po_box = sql_reader["address_po_box"].ToString();

    address_governorate = sql_reader["address_governorate"].ToString();

    address_outside_address = sql_reader["address_outside_address"].ToString();

    contact_moblle = sql_reader["contact_moblle"].ToString();

    contact_home_tel = sql_reader["contact_home_tel"].ToString();

    contact_home_fax = sql_reader["contact_home_fax"].ToString();

    email_address = sql_reader["email_address"].ToString();

    profession = sql_reader["profession"].ToString();

    company = sql_reader["company"].ToString();

    contact_office_tel = sql_reader["contact_office_tel"].ToString();

    contact_ext_direct = sql_reader["contact_ext_direct"].ToString();

    contact_office_fax = sql_reader["contact_office_fax"].ToString();

    }

    2006年11月11日 上午 12:41
  • add column names to select statement on your sp_get_person_by_file Procedure

    ex.
    address_po_box As AddressPoBox


    Then your code will be
    txtBox.Text = sql_reader["AddressPoBox"].ToString();
    2008年10月29日 上午 06:47
  • We are also facing this kind of issue.

    Can you explain the reason behind this fix?

    Regards,

    Nakul


    Regards, Nakul

    2018年10月9日 上午 11:49
  • The cause is because you are attempting to access a column that doesn't exist in the result set returned from SQL Server. Double-check your result set using SSMS.

    ~~Bonnie DeWitt [C# MVP]

    http://geek-goddess-bonnie.blogspot.com

    2018年10月9日 下午 02:13
  • We are also facing this kind of issue.

    Can you explain the reason behind this fix?

    Regards,

    Nakul

    I'm not sure from whom you were asking for this information, as you haven't
    included any lines from a prior post to give context to your reply.

    However, note that the original problem description was posted twelve (12)
    years ago. The suggested fix by Richmond Carlo Teng was posted two years
    later and is itself 10 years old.

    Tacking a request for more info onto the end of a 10-12 year old thread
    is rarely if ever a good idea. You should start a new thread of your own
    and describe your problem in detail. Which you appear to have started to 
    do at

    https://social.msdn.microsoft.com/Forums/vstudio/en-US/c051d793-84bc-43e2-b97f-fc0dfe0ef9ce/sql-datareader-indexoutofrangeexception-at-systemdataproviderbasefieldnamelookupgetordinal?forum=csharpgeneral

    You should specify what version(s) of software you are using. Threads
    that are many years old were probably posted by someone using much
    older versions of software. Problems and solutions can and often do 
    differ from version to version.

    - Wayne

    2018年10月9日 下午 07:49