TypeIntialization Exception Was Unhandled...PLzzz help me...........

  • 12 years ago

    Hello,

             Can anyone  plzz solve my  prblm... Below  code is for Immigration_Details of the employee.....

     i got an Exception while running....

    Exception : TypeIntialization Exception was Unhandled...........

    But i didn't change the Form Name in this appln...

    Please help me..........

     

    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;
    using System.IO;

    namespace Immigration_Details1
    {
        public partial class Form1 : Form
        {
            public Form1()
            {
                InitializeComponent();
            }
            static SqlConnection cn = new SqlConnection("Provider=SQLOLEDB.1;Integrated Security=SSPI;Persist Security Info=False;Initial Catalog=EMP;Data Source=user");
            DataSet ds = new DataSet();
            SqlDataReader rdr;
            DataTable dt;
            DataRow dr;
            int i;
            private void Form1_Load(object sender, EventArgs e)
            {
                SqlCommand cmd = new SqlCommand("select empid,immigrationstatus,firstname,middilename,lastname from Employee_Details", cn);
                cn.Open();
                rdr = cmd.ExecuteReader();
                if (rdr.Read())
                {
                    empid_txt.Text = rdr["empid"].ToString();
                    immigrationstatus_cmb.Text = rdr["immigrationstatus"].ToString();
                    fname_txt.Text = rdr["firstname"].ToString();
                    mname_txt.Text = rdr["middilename"].ToString();
                    lname_txt.Text = rdr["lastname"].ToString();
                }

                if (immigrationstatus_cmb.SelectedIndex == 0)
                {
                    immg_groupBox1.Visible = true;
                    Nonimmg_groupBox2.Visible = false;
                    Immg_VisaDetails();
                }
                if (immigrationstatus_cmb.SelectedIndex == 1)
                {
                    Nonimmg_groupBox2.Visible = true;
                    immg_groupBox1.Visible = false;
                    NonImmg_VisaDetails();
                }
            }

            private void Immg_VisaDetails()
            {
                string q = "select receiptnumber,dateapplied,dateapproved from Immigrant_Details where greencarddetails='" + greencarddetails_cmb.SelectedIndex.ToString() + "'";
                SqlCommand cmnd = new SqlCommand(q, cn);
                SqlDataReader Rdr;
                cn.Open();
                Rdr = cmnd.ExecuteReader();
                while (Rdr.Read())
                {
                    receiptnumber_txt.Text = Rdr["receiptnumber"].ToString();
                    dateapplied_txt.Text = Rdr["dateapplied"].ToString();
                    dateapproved_txt.Text = Rdr["dateapproved"].ToString();
                }
            }

            private void NonImmg_VisaDetails()
            {
                SqlDataAdapter Sda = new SqlDataAdapter("select * from NonImmigrant_Details", cn);
                Sda.Fill(ds, "NonImmigration_Details");
                dt = ds.Tables["NonImmigration_Details"];
                dr = dt.Rows[i];
                visatype_cmb.Text = dr[0].ToString();
                visastartdate_txt.Text = dr[1].ToString();
                visaexpirationdate_txt.Text = dr[2].ToString();
            }

        }
    }

     

    Advance thnx 

     

     

     

  • 12 years ago

    Can you put the exception StackTrace here? In IDE you can also examine the InnerException property in the Locals window 

  • 12 years ago

    Hi,

       What is exception StackTrace ??? , i tried it by checking the InnerException Property Window but i didn't get it....

    I got this Error in Program.CS

    using System;
    using System.Collections.Generic;
    using System.Windows.Forms;

    namespace Immigration_Details1
    {
        static class Program
        {
            /// <summary>
            /// The main entry point for the application.
            /// </summary>
            [STAThread]
            static void Main()
            {
                Application.EnableVisualStyles();
                Application.SetCompatibleTextRenderingDefault(false);
                Application.Run(new Form1()); ------>>> Exception Occured Here
            }
        }
    }
     

    please help me..........

     

    Thnqqqqq 

  • 12 years ago

    Hi,

    In NonImmg_VisaDetails()----method , you are trying to assign the the table from dataset i.e

    dt = ds.Tables["NonImmigration_Details"];

    I think this is the line of code which was throwing exception

    try  doing below:

    dt = new DataTable();

    dt = ds.Tables["NonImmigration_Details"];

    Also Initialize the datarow before using...

     

    Thanks, 

     

Post a reply

Enter your message below

Sign in or Join us (it's free).

Contribute

Why not write for us? Or you could submit an event or a user group in your area. Alternatively just tell us what you think!

Our tools

We've got automatic conversion tools to convert C# to VB.NET, VB.NET to C#. Also you can compress javascript and compress css and generate sql connection strings.

“Debuggers don't remove bugs. They only show them in slow motion.”