none
Object reference not set to an instance of an object. Please Help! RRS feed

  • Question

  • using System;
    using System.Collections.Generic;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Windows.Forms;
    using System.Data.SqlClient;
    using System.IO;
    using System.Configuration;

    namespace Finance_and_Cost_Controlling_System
    {
        public partial class Customer : Form
        {
            static string constring = ConfigurationManager.ConnectionStrings["Finance_and_Cost_Controlling_System.Properties.Settings.Setting"].ConnectionString;
             SqlConnection con = new SqlConnection(constring);
            //SqlConnection con = new SqlConnection("Data Source = KHALIDKK; initial Catalog = SIGBL; integrated security = false; User Id = sa; Password= 02136660711...; Timeout = 45; MultipleActiveResultSets=True;");
            SqlCommand command;

            public Customer()
            {
                InitializeComponent();
            }

            private void tableLayoutPanel1_Paint(object sender, PaintEventArgs e)
            {

            }

            private void button2_Click(object sender, EventArgs e)
            {
                if (panel1.Visible == false)
                {

                    bunifuTransition1.ShowSync(panel1);
                    panel1.Visible = true;
                    panel1.Width = 1184;
                    button3.Enabled = true;
                }
                else
                {

                    bunifuTransition1.ShowSync(panel1);
                    panel1.Visible = false;
                    panel1.Width = 10;
                }
            }





            private void button3_Click(object sender, EventArgs e)
            {
                panel1.Hide();
            }

            private void button1_Click(object sender, EventArgs e)
            {
                MessageBox.Show("Inserted");
               try
                {
                    if (con.State != ConnectionState.Open)
                        con.Open();


                    string conn = "INSERT INTO Customer (Firstname,Lastname,Email,Company,Phone,Mobile,Other,Website,Fax,Street1,Street2,Country,City,ProvinceState,Notes,Postcode)VALUES('" + textBox1.Text + "','" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "', '" + comboBox1.SelectedItem.ToString() + "', '" + textBox12.Text + "', '" + comboBox2.SelectedItem.ToString() + "','" + richTextBox1.Text + "', '" + textBox13.Text + "')";
                    command = new SqlCommand(conn, con);

                    int x = command.ExecuteNonQuery();
                    con.Close();
                    MessageBox.Show(x.ToString() + "record(s) saved.");
                }
                catch(Exception ex)
                {
                    MessageBox.Show(ex.Message);
                }
            }
        }
    }

                                                    
    • Edited by Khalid kk Tuesday, June 25, 2019 11:15 PM
    Tuesday, June 25, 2019 11:13 PM

All replies

  • Greetings Khalid.

    This would be a lot easier if you could tell us which line the error occurs on. Best guess is that 'con' should not be defined as local in the constructor.

            static string constring = ConfigurationManager.ConnectionStrings["Finance_and_Cost_Controlling_System.Properties.Settings.Setting"].ConnectionString;
             //SqlConnection con = new SqlConnection(constring); // Replace this line with the one below.
            con = new SqlConnection(constring);
    
            //SqlConnection con = new SqlConnection("Data Source = KHALIDKK; initial Catalog = SIGBL; integrated security = false; User Id = sa; Password= 02136660711...; Timeout = 45; MultipleActiveResultSets=True;");
            SqlCommand command;

    Tuesday, June 25, 2019 11:26 PM
  • Hello,

    A couple of suggestions.

    • Don't use a single connection for database operations, instead create a connection as needed. See the following for examples.
    • Learn how to set breakpoints and step though code to determine what occurred and the state of objects that may be part of the problem then trace back to see how to correct the issue. Visual Studio debugging is easy to get started with if one takes the time to learn how to debug.

    The above can help you help yourself.


    Please remember to mark the replies as answers if they help and unmarked them if they provide no help, this will help others who are looking for solutions to the same or similar problem. Contact via my Twitter (Karen Payne) or Facebook (Karen Payne) via my MSDN profile but will not answer coding question on either.

    NuGet BaseConnectionLibrary for database connections.

    StackOverFlow
    profile for Karen Payne on Stack Exchange

    Wednesday, June 26, 2019 12:49 AM
    Moderator
  • Hi Khalid kk,

    Thank you for posting here.

    I have tested the code, it works. I think the exception is related with the connectionstring. You could set break point to see which line generated the exception.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Wednesday, June 26, 2019 5:48 AM
    Moderator
  • Hi Ante Meridian

    AT this line Exception generate 

    string sql = "INSERT INTO Customer (Firstname,Lastname,Email,Company,Phone,Mobile,Other,Website,Fax,Street1,Street2,Country,City,ProvinceState,Notes,Postcode)VALUES('" + textBox1.Text + "','" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "', '" + comboBox1.SelectedItem.ToString() + "', '" + textBox12.Text + "', '" + comboBox2.SelectedItem.ToString() + "','" + richTextBox1.Text + "', '" + textBox13.Text + "')";


    • Edited by Khalid kk Wednesday, June 26, 2019 8:33 PM
    Wednesday, June 26, 2019 8:32 PM
  • Hi Jack

    AT this line Exception generate 

    string sql = "INSERT INTO Customer (Firstname,Lastname,Email,Company,Phone,Mobile,Other,Website,Fax,Street1,Street2,Country,City,ProvinceState,Notes,Postcode)VALUES('" + textBox1.Text + "','" + textBox2.Text + "', '" + textBox3.Text + "', '" + textBox4.Text + "', '" + textBox5.Text + "', '" + textBox6.Text + "','" + textBox7.Text + "','" + textBox8.Text + "','" + textBox9.Text + "','" + textBox10.Text + "','" + textBox11.Text + "', '" + comboBox1.SelectedItem.ToString() + "', '" + textBox12.Text + "', '" + comboBox2.SelectedItem.ToString() + "','" + richTextBox1.Text + "', '" + textBox13.Text + "')";

    Wednesday, June 26, 2019 8:35 PM
  • If anyone of the UI control objects is a null valued object, then you are going to get the error message you are getting of object not set to an instance of an object.

    The controls are objects that work on the principles in the link. If the object was not instanced and setting in memory, then it's a null valued object that is not there in memory, and code is referencing an object that is not instanced living in memory.  

    Who is the nulled value object? You can hover over each object in debug mode with the mouse pointer and be shown what object is a null valued object, or use Quickwatch. 

    OO is OO in .NET or Java.

    https://alfredjava.wordpress.com/2008/07/08/class-vs-object-vs-instance/



    • Edited by DA924x Wednesday, June 26, 2019 9:20 PM
    Wednesday, June 26, 2019 9:19 PM
  • Hi Khalid,

    Thanks for the feedback.

    Could you send this project and database design via Onedrive? I think it's best way for us to reproduce and solve your problem.

    Best Regards,

    Jack


    MSDN Community Support
    Please remember to click "Mark as Answer" the responses that resolved your issue, and to click "Unmark as Answer" if not. This can be beneficial to other community members reading this thread. If you have any compliments or complaints to MSDN Support, feel free to contact MSDNFSF@microsoft.com.

    Thursday, June 27, 2019 8:18 AM
    Moderator