0
using (SqlConnection connection = new SqlConnection(DataHelper.ConnectionString))
        {
            using (SqlDataAdapter Adapter = new SqlDataAdapter())
            {

                try
                {
                    Adapter.InsertCommand.Connection.Open();

                    Adapter.InsertCommand = new SqlCommand("Sample", connection);
                    Adapter.InsertCommand.CommandType = CommandType.StoredProcedure;

                    Adapter.InsertCommand.Parameters.AddWithValue("@reqno", "emid");
                    Adapter.InsertCommand.Parameters.AddWithValue("@vouno", "emname");

                    Adapter.InsertCommand.ExecuteNonQuery();
                   
                }
                catch (Exception aa)
                {
                    MessageBox.Show("x " + aa);
                }
[B] Adapter.InsertCommand.Connection.Close();[/B] // It Shows error in here
            }

Edited by __avd: Added [code] .[/code] tags.

2
Contributors
1
Reply
2
Views
6 Years
Discussion Span
Last Post by Knvn
0

This error occurs, if the object which you are referring is Null. So, modify your last code block to:

catch (Exception aa)
{
    MessageBox.Show("x " + aa);
}
finally
{
    if (Adapter.InsertCommand.Connection != null)
        Adapter.InsertCommand.Connection.Close();
}
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.