i am getting "Unhandled Null referenceException "--object reference not set to an instance of an object at below mentioned code in bracket

(return System.Configuration.ConfigurationManager.ConnectionStrings["unitmoduleconnectionstring"].ConnectionString)
and below is the total code

help me to get out of this bug....

namespace TIMS
{
    public partial class Form1: Form
    {

        public Form1()
        {
            InitializeComponent();
        }

        private void itemmastBindingNavigatorSaveItem_Click(object sender, EventArgs e)
        {
            this.Validate();
            this.itemmastBindingSource.EndEdit();
            this.tableAdapterManager.UpdateAll(this.unitmoduleDataSet);
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            // TODO: This line of code loads data into the 'unitmoduleDataSet.itemmast' table. You can move, or remove it, as needed.
            this.itemmastTableAdapter.Fill(this.unitmoduleDataSet.itemmast);

        }

        public string GetConnectionString()
        {
            return System.Configuration.ConfigurationManager.ConnectionStrings["unitmoduleconnectionstring"].ConnectionString;
        }  

        System.Data.SqlClient.SqlConnection con;
        System.Data.SqlClient.SqlDataAdapter da;
        System.Data.SqlClient.SqlCommand cmd;
        DataSet ds1;        
        int inc = 0;
        private void BtnSave_Click(object sender, EventArgs e)
        {
            {
                 InsertInfo();      
            }
        }

        private void InsertInfo()
         {
         System.Data.SqlClient.SqlConnection con = new System.Data.SqlClient.SqlConnection(GetConnectionString());

              string sql = "INSERT INTO itemmast (itemcode,itemName,iunitmeasure,itemtype,Createdby,Unitcode) VALUES (@Val1,@Val2,@Val3,@Val4,@Val5,@Val6)";
                try
                {
                    con.Open();
                    cmd = new System.Data.SqlClient.SqlCommand(sql, con);
                    cmd.Parameters.AddWithValue("@Val1", Itemcode.Text);
                    cmd.Parameters.AddWithValue("@Val2", ItemName.Text);
                    cmd.Parameters.AddWithValue("@Val3", UnitMeasure.Text);
                    cmd.Parameters.AddWithValue("@Val1", Itemtype.Text);
                    cmd.Parameters.AddWithValue("@Val1", Username.Text);
                    cmd.Parameters.AddWithValue("@Val1", MDIunitcode.Text);
                    cmd.CommandType = CommandType.Text;
                    cmd.ExecuteNonQuery();
                }
                catch (SqlException ex)
                {
                    MessageBox.Show("There is an Error" + ex);
                }
                finally
                {
                    con.Close();
                    MessageBox.Show("Connection Closed");
                }
}       

You are giving 4 of the parameters the same name. Without a doubt this will cause problems - I think you meant to increment each one but stopped at @Val3. Since you are creating this command and only running it once, you don't really need to use parameters, just a simple string with your values in it would work just fine.

As for the exception - where is this connection string being explicitly set (or is it being set at all?)

Is the name of you unit moduleconnectionstring correct in your config file? It looks like, for the some reason, the connection isn't being returned. Easiest thing to do is debug the function that calls the string and see what the output is.
Also, as you're dealing with unfiltered user input, using parameters in your query statement is OK. You should never give the user the ability to type text that will end up being directly entered into a query (it called SQl injection).
Well spotted with the copied parameter names, Skatamatic. Copy and paste gets us all at some point:)

You write same statement which cause error for other variables of Insert statement

                cmd.Parameters.AddWithValue("@Val1", Itemtype.Text);
                cmd.Parameters.AddWithValue("@Val1", Username.Text);
                cmd.Parameters.AddWithValue("@Val1", MDIunitcode.Text);
Member Avatar for prathamesh3090

Please help me,

I want to insert data of form 1 using form 2 into sql table.
Please give me the code for the same.

Close this thread (if this is the thread you created).
And create a new one.

Member Avatar for prathamesh3090

how to create new thread please help

Member Avatar for prathamesh3090

I am trying to insert dattime of masked textbox into sql table having datatype datetime but it fails please help ( i am new to daniweb)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.