problem in insert of gridview

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • raghulvarma
    New Member
    • Oct 2007
    • 90

    problem in insert of gridview

    I have got a grid view in that I have two label and then a checkbox.
    In presentation layer ie .cs i have the coding like
    Code:
    foreach(GridViewRow row in GridView1.Rows)
    {
        if(row.RowType == DataControlRowType.DataRow)
             {
                empid = int.parse(((Label)row.FindControl("Lbl1")).Text);
                name = (((Label)row.FindControl("Lbl2")).Text);
                bool ischecked = (((CheckBox)row.FindControl("CheckBox")).Checked);
                if(ischecked)
                         {
                                  status = "Present"
                         }
                else
                         {
                                 status = "Absent"
                         }
             }
    }
    in Data Access Layer I have
    Code:
    try
    {
    connection.Open();
    cmd = new SqlCommand("sp for insert",connection);
    cmd.CommandType = CommandType.StoredProcedure;
    cmd.Parameters.AddWithValue("@empid",empid);
    cmd.Parameters.AddWithValue("@name",name);
    cmd.Parameters.AddWithValue("@status",status);
    }
    catch
    {
    throw;
    }
    in Business Access layer

    the same story

    now the problem that arise is that
    1. condition for the checkbox is not done,the values is always "absent " even if we check the checkbox
    2. the values gets inserted only for the first time and in the second time and third time it shows an error as object reference not set to as instance.
    what has to be done?

    Thanks in advance
    Raghul
  • madankarmukta
    Contributor
    • Apr 2008
    • 308

    #2
    HI,

    For you first query I would suggest you to check the binding of the the datagrid column .. there might be mismatch in the name with which you are binding the
    checkbox and the columnName in the dataTable/DataSet with which you did binding of your grid.. Clear ..?

    Now come to the second point..
    Exactly where you are getting this exception ..? Could you please send me the callstack function calling sequence..?

    Thanks!

    Comment

    • Frinavale
      Recognized Expert Expert
      • Oct 2006
      • 9749

      #3
      Are you doing a DataBind on your GridView in your PageLoad event?

      -Frinny

      Comment

      • raghulvarma
        New Member
        • Oct 2007
        • 90

        #4
        Originally posted by Frinavale
        Are you doing a DataBind on your GridView in your PageLoad event?

        -Frinny
        Yes
        I bind the data in gridview in the PageLoad, and as I have said i have a checkbox in the gridview and I need to check if it has been checked or not in the Button Click.
        My first problem is that it shows a "System.NullRef erenceException : Object reference not set to an instance of an object."
        the first record gets inserted but when the control comes to the second record the error arises any clues?

        Comment

        • Frinavale
          Recognized Expert Expert
          • Oct 2006
          • 9749

          #5
          Originally posted by raghulvarma
          Yes
          I bind the data in gridview in the PageLoad, and as I have said i have a checkbox in the gridview and I need to check if it has been checked or not in the Button Click.
          My first problem is that it shows a "System.NullRef erenceException : Object reference not set to an instance of an object."
          the first record gets inserted but when the control comes to the second record the error arises any clues?

          Only do a DataBind on your GridView if it is not a PostBack.

          If you do a DataBind on your GridView in your Page load event the data source for the GridView is deleted and reset....all events and objects that would have been made available to you will also be deleted and you will see your NullReference error as soon as you try to access one of these objects.....

          Comment

          • raghulvarma
            New Member
            • Oct 2007
            • 90

            #6
            Originally posted by Frinavale
            Only do a DataBind on your GridView if it is not a PostBack.

            If you do a DataBind on your GridView in your Page load event the data source for the GridView is deleted and reset....all events and objects that would have been made available to you will also be deleted and you will see your NullReference error as soon as you try to access one of these objects.....
            I have bind the gridview only if the postback is false.but as I have told you only for the first time the first record gets inserted and after that only the null reference occours.

            let me send you my coding which I have done
            in Data access layer I have
            Code:
            public int InsertData(int empid, string empname, string status, string currentdate, string currentmonth)
                {
                    try
                    {
                        string SqlString = "insert into table1 (ID,EmpName,Status,CurrentDate,CurrentMonth) values (?,?,?,?,?)";
                        con = new OleDbConnection(Connection);
                        con.Open();
                        command = new OleDbCommand(SqlString, con);
                        command.CommandType = CommandType.Text;
                        command.Parameters.AddWithValue("ID", empid);
                        command.Parameters.AddWithValue("EmpName", empname);
                        command.Parameters.AddWithValue("Status", status);
                        command.Parameters.AddWithValue("CurrentDate", currentdate);
                        command.Parameters.AddWithValue("CurrentMonth", currentmonth);
                        return command.ExecuteNonQuery();
            
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        command.Dispose();
                        con.Close();
                        con.Dispose();
                    }
                }
            in Business access layer
            Code:
             public int InsertData(int empid, string empname, string status, string currentdate, string currentmonth)
                {
                    try
                    {
                       return obj.InsertData(empid, empname, status, currentdate, currentmonth);
                    }
                    catch
                    {
                        throw;
                    }
                    finally
                    {
                        obj = null;
                    }
                }
            and in presentation layer,
            I insert the all records in the single button event as
            Code:
            protected void Button1_Click(object sender, EventArgs e)
                {
                   
                       foreach (GridViewRow row in GridView1.Rows)
                       {
                           if (row.RowType == DataControlRowType.DataRow)
                           {
                               empid = int.Parse(((Label)row.FindControl("lbl1")).Text);
                               empname = ((Label)row.FindControl("lbl2")).Text;
                               bool ischecked = ((CheckBox)row.FindControl("chkbox1")).Checked;
                               if (ischecked)
                               {
                                   status = "Present";
                               }
                               else
                               {
                                   status = "Absent";
                               }
                               currentdate = TextBox1.Text;
                               currentmonth = TextBox2.Text;
                               try
                               {
                                   obj1.InsertData(empid, empname, status, currentdate, currentmonth);
                               }
                               catch (Exception ee)
                               {
                                   Response.Write(ee.ToString());
                               }
                               finally
                               {
                                   obj1 = null;
                               }
                           
                       }
                    }
            what is the problem and also the other problem is that even if the checkbox is checked the bool value returns false what has to be done?

            Comment

            • Frinavale
              Recognized Expert Expert
              • Oct 2006
              • 9749

              #7
              Originally posted by raghulvarma
              Code:
                         foreach (GridViewRow row in GridView1.Rows)
                         {
                             if (row.RowType == DataControlRowType.DataRow)
                             {
                                 empid = int.Parse(((Label)row.FindControl("lbl1")).Text);
                                 empname = ((Label)row.FindControl("lbl2")).Text;
                                 bool ischecked = ((CheckBox)row.FindControl("chkbox1")).Checked;
                                 if (ischecked)
                                 {
                                     status = "Present";
                                 }
                                 else
                                 {
                                     status = "Absent";
                                 }
                                 currentdate = TextBox1.Text;
                                 currentmonth = TextBox2.Text;
                                 try
                                 {
                                     obj1.InsertData(empid, empname, status, currentdate, currentmonth);
                                 }
                                 catch (Exception ee)
                                 {
                                     Response.Write(ee.ToString());
                                 }
                                 finally
                                 {
                                     obj1 = null;
                                 }
                             
                         }
                      }
              In your for each loop you are setting obj1 = null in the Finally block.
              Please note that if a finally clause is included in a try catch block, it's statements are executed after all other try-catch processing is complete.

              Since you are setting obj1 = null and then you are attempting use it to insert the data you will receive a Null Reference error.

              Either remove this from your finally block or re-instantiate the obj1 at the top of your loop.

              Comment

              Working...