Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. General Programming
  3. C#
  4. Not all code paths return a value

Not all code paths return a value

Scheduled Pinned Locked Moved C#
help
5 Posts 4 Posters 0 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • S Offline
    S Offline
    sheringkapoting
    wrote on last edited by
    #1
    private DataTable AddNewRowToGrid()
    {
        int rowIndex = 0;
        if (ViewState\["CurrentTable"\] != null)
        {
            DataTable dtCurrentTable = (DataTable)ViewState\["CurrentTable"\];
            DataRow drCurrentRow = null;
            if (dtCurrentTable.Rows.Count > 0)
            {
                for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                {
                    //extract the TextBox values
                    drCurrentRow = dtCurrentTable.NewRow();
                    dtCurrentTable.Rows\[i - 1\]\["EffectFrom"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[1\].FindControl("txtEffectFrom")).Text;
                    dtCurrentTable.Rows\[i - 1\]\["EffectivePercentage"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[2\].FindControl("txtEffectivePercentage")).Text;
                    dtCurrentTable.Rows\[i - 1\]\["Grade\_Salary"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[3\].FindControl("txtGradeSalary")).Text;
                    rowIndex++;
                }
                dtCurrentTable.Rows.Add(drCurrentRow);
                ViewState\["CurrentTable"\] = dtCurrentTable;
                return dtCurrentTable;
            }
        }
    }
    

    This code gives me the Fallowing Error: Not all code paths return a value plss Help !!

    C P A 3 Replies Last reply
    0
    • S sheringkapoting
      private DataTable AddNewRowToGrid()
      {
          int rowIndex = 0;
          if (ViewState\["CurrentTable"\] != null)
          {
              DataTable dtCurrentTable = (DataTable)ViewState\["CurrentTable"\];
              DataRow drCurrentRow = null;
              if (dtCurrentTable.Rows.Count > 0)
              {
                  for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                  {
                      //extract the TextBox values
                      drCurrentRow = dtCurrentTable.NewRow();
                      dtCurrentTable.Rows\[i - 1\]\["EffectFrom"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[1\].FindControl("txtEffectFrom")).Text;
                      dtCurrentTable.Rows\[i - 1\]\["EffectivePercentage"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[2\].FindControl("txtEffectivePercentage")).Text;
                      dtCurrentTable.Rows\[i - 1\]\["Grade\_Salary"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[3\].FindControl("txtGradeSalary")).Text;
                      rowIndex++;
                  }
                  dtCurrentTable.Rows.Add(drCurrentRow);
                  ViewState\["CurrentTable"\] = dtCurrentTable;
                  return dtCurrentTable;
              }
          }
      }
      

      This code gives me the Fallowing Error: Not all code paths return a value plss Help !!

      C Offline
      C Offline
      Can SARIGUL
      wrote on last edited by
      #2

      insert return null; before the last }

      S 1 Reply Last reply
      0
      • S sheringkapoting
        private DataTable AddNewRowToGrid()
        {
            int rowIndex = 0;
            if (ViewState\["CurrentTable"\] != null)
            {
                DataTable dtCurrentTable = (DataTable)ViewState\["CurrentTable"\];
                DataRow drCurrentRow = null;
                if (dtCurrentTable.Rows.Count > 0)
                {
                    for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                    {
                        //extract the TextBox values
                        drCurrentRow = dtCurrentTable.NewRow();
                        dtCurrentTable.Rows\[i - 1\]\["EffectFrom"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[1\].FindControl("txtEffectFrom")).Text;
                        dtCurrentTable.Rows\[i - 1\]\["EffectivePercentage"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[2\].FindControl("txtEffectivePercentage")).Text;
                        dtCurrentTable.Rows\[i - 1\]\["Grade\_Salary"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[3\].FindControl("txtGradeSalary")).Text;
                        rowIndex++;
                    }
                    dtCurrentTable.Rows.Add(drCurrentRow);
                    ViewState\["CurrentTable"\] = dtCurrentTable;
                    return dtCurrentTable;
                }
            }
        }
        

        This code gives me the Fallowing Error: Not all code paths return a value plss Help !!

        P Offline
        P Offline
        Pete OHanlon
        wrote on last edited by
        #3

        The reason it's given you this message is because you have the condition to test if the ViewState is not null. What happens if ViewState["CurrentTable"] is null? (You have a similar issue if dtCurrentTable.Rows.Count == 0. To fix this, return an empty DataTable from the method (most style guides recommend not returning null).

        *pre-emptive celebratory nipple tassle jiggle* - Sean Ewington

        "Mind bleach! Send me mind bleach!" - Nagy Vilmos

        CodeStash - Online Snippet Management | My blog | MoXAML PowerToys | Mole 2010 - debugging made easier

        1 Reply Last reply
        0
        • C Can SARIGUL

          insert return null; before the last }

          S Offline
          S Offline
          sheringkapoting
          wrote on last edited by
          #4

          Can SARIGÜL wrote:

          insert return null; before the last }

          not working actually I want this Function result to be used as Datatable to fill a Gridview. return null will work

          1 Reply Last reply
          0
          • S sheringkapoting
            private DataTable AddNewRowToGrid()
            {
                int rowIndex = 0;
                if (ViewState\["CurrentTable"\] != null)
                {
                    DataTable dtCurrentTable = (DataTable)ViewState\["CurrentTable"\];
                    DataRow drCurrentRow = null;
                    if (dtCurrentTable.Rows.Count > 0)
                    {
                        for (int i = 1; i <= dtCurrentTable.Rows.Count; i++)
                        {
                            //extract the TextBox values
                            drCurrentRow = dtCurrentTable.NewRow();
                            dtCurrentTable.Rows\[i - 1\]\["EffectFrom"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[1\].FindControl("txtEffectFrom")).Text;
                            dtCurrentTable.Rows\[i - 1\]\["EffectivePercentage"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[2\].FindControl("txtEffectivePercentage")).Text;
                            dtCurrentTable.Rows\[i - 1\]\["Grade\_Salary"\] = ((TextBox)mdlpopupGrid.Rows\[rowIndex\].Cells\[3\].FindControl("txtGradeSalary")).Text;
                            rowIndex++;
                        }
                        dtCurrentTable.Rows.Add(drCurrentRow);
                        ViewState\["CurrentTable"\] = dtCurrentTable;
                        return dtCurrentTable;
                    }
                }
            }
            

            This code gives me the Fallowing Error: Not all code paths return a value plss Help !!

            A Offline
            A Offline
            Apocalypse Now
            wrote on last edited by
            #5

            Add: else { return null; } before last '}'.

            1 Reply Last reply
            0
            Reply
            • Reply as topic
            Log in to reply
            • Oldest to Newest
            • Newest to Oldest
            • Most Votes


            • Login

            • Don't have an account? Register

            • Login or register to search.
            • First post
              Last post
            0
            • Categories
            • Recent
            • Tags
            • Popular
            • World
            • Users
            • Groups