Click here to Skip to main content
15,917,568 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
This is a method for Save. When I debug the method it brings data from textbox and gridview and show it in DTLocal data table. After that it does not go to the next line. It goes to catch and arises an error "Connection not Initialized". Here NewSparePartsCode() generate a code. Please help me.
C#
private void SaveData()
    {
        DataTable DTLocal = null;
        DataView DVLocal = null;
        DataRow DRLocal = null;
        System.Web.UI.Page this_page_ref = null;
        string strSQL = "";
        try
        {
            DTLocal = new DataTable();
            DVLocal = new DataView();

            strSQL = "select * from tblSpareParts where SPCode='" + txtSparePartsCode.Text.Trim() + "'";
            ConManager.DBConnection("TERMSHR");
            ConManager.OpenDataTableThroughAdapter(strSQL, out DTLocal, true);
            DTLocal.TableName = "SpareParts";
            ConManager.CloseConnection();
            DVLocal.Table = DTLocal;

            if (DVLocal.Count == 0)
            {
                for (int i = 0; i < grdViewSpareParts.Rows.Count; i++)
                {
                    NewSparePartsCode();
                    DRLocal = DTLocal.NewRow();
                    DRLocal["SPCode"] = txtSparePartsCode.Text.Trim();
                    DRLocal["MTCode"] = grdViewSpareParts.Rows[i].Cells[1].Text.Trim();
                    DRLocal["SpareParts"] = grdViewSpareParts.Rows[i].Cells[2].Text.Trim();

                    DTLocal.Rows.Add(DRLocal);

               }
            }

            else
            {
                for (int i = 0; i < grdViewSpareParts.Rows.Count; i++)
                {
                    DRLocal = DVLocal[0].Row;
                    DRLocal.BeginEdit();
                    CheckBox cb = (CheckBox)grdViewSpareParts.Rows[i].Cells[0].FindControl("chkCheck");
                    if (cb.Checked)
                    {
                        DRLocal["SPCode"] = txtSparePartsCode.Text.Trim();
                        DRLocal["MTCode"] = grdViewSpareParts.Rows[i].Cells[1].Text.Trim();
                        DRLocal["SpareParts"] = grdViewSpareParts.Rows[i].Cells[2].Text.Trim();
                    }
                    DRLocal.EndEdit();
                }
            }
            ConManager.DBConnection("TERMSHR");
            ConManager.SaveDataTableThroughAdapter(ref DTLocal, true);
            ConManager.CloseConnection();

        }

        catch (Exception ex)
        {
            throw ex;
        }
        finally
        {
            //
        }

    }
Posted
Updated 5-May-13 1:00am
v2
Comments
Maciej Los 5-May-13 7:21am    
Does ConManager object is Connection Manager[^]?
Exactly on which line you are getting the exception ?
vaibhav10Dec1987 6-May-13 7:13am    
Do not close your connection at the middle of the function, it close only at the end of the function.
ZurdoDev 6-May-13 10:30am    
The error is clear. Somewhere in your custom classes the connection is not getting initialized like it should.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900