Error: - Object reference not set to an instance of an object.

Jan 26 2009 2:14 PM
 public string AcceptDetails(string ISBN, string DateOrder, string CustName, string CustAddr1, string CustAddr2, string CustCity, string CustState, string OrdBy, string OrdStat, string CardType, string CardNum)
        {
            string OrderNo;
            string error;
            error = "";
            OrderNo = GenerateOrder();
            string InsStr;
            InsStr = "insert into dtorders values (@IN, @ON, @DO, @CN, @CA1, @CA2, @CC, @CS, @OB, @ST, @CT, @CNN)";
            try
            {
                SqlConnection conn = new SqlConnection("server=HOME-1E79531B32;database=DTDB;trusted_connection=yes");
                SqlCommand cmd1 = new SqlCommand(InsStr,conn);
                SqlDataAdapter dad1 = new SqlDataAdapter(cmd1);
                dad1.InsertCommand.Parameters.Add("@IN", SqlDbType.Char, 10).Value = ISBN;
                dad1.InsertCommand.Parameters.Add("@ON",SqlDbType.Char, 5).Value = OrderNo;
                dad1.InsertCommand.Parameters.Add("@DO", SqlDbType.DateTime, 8).Value = Convert.ToDateTime(DateOrder).Date;
                dad1.InsertCommand.Parameters.Add("@CN", SqlDbType.VarChar, 50).Value = CustName;
                dad1.InsertCommand.Parameters.Add("@CA1", SqlDbType.VarChar, 50).Value = CustAddr1;
                dad1.InsertCommand.Parameters.Add("@CA2", SqlDbType.VarChar, 50).Value = CustAddr2;
                dad1.InsertCommand.Parameters.Add("@CC", SqlDbType.VarChar, 20).Value = CustCity;
                dad1.InsertCommand.Parameters.Add("@CS", SqlDbType.VarChar, 10).Value = CustState;
                dad1.InsertCommand.Parameters.Add("@OB", SqlDbType.VarChar, 50).Value = OrdBy;
                dad1.InsertCommand.Parameters.Add("@ST", SqlDbType.VarChar, 20).Value = OrdStat;
                dad1.InsertCommand.Parameters.Add("@CT", SqlDbType.Char, 10).Value = CardType;
                dad1.InsertCommand.Parameters.Add("@CNN", SqlDbType.VarChar, 20).Value = CardNum;

                if (conn.State == ConnectionState.Closed)
                {
                    conn.Open();
                }
                dad1.InsertCommand.ExecuteNonQuery();
                conn.Close();

            }

            catch(Exception E1)
            {
                error = E1.Message;
            }

            string result;
            if (error.Length != 0)
            {
                result = "Record not Inserted due to the following reason: \n" + error;
            }

            else
            {
                result = "Record Inserted!!";
            }
            return result;
        }

Error: - Object reference not set to an instance of an object.


Answers (2)