Error: not all code paths return a value

Last Reply on Nov 10, 2016 03:21 AM By dharmendr

Posted on Nov 10, 2016 03:17 AM

Merging procedure on one datalist showing error

 

protected void Page_Load(object sender, EventArgs e)
    {

        if (!Page.IsPostBack)
        {
            if (this.Page.User.Identity.IsAuthenticated)
            {
                username = this.Page.User.Identity.Name;
             
                DataTable dt = GetProfile(username);
                               
                        GetMergedAll.DataSource = dt;
                ViewState["DataTable"] = dt;
                        GetMergedAll.DataBind();                  
              
                if (dt.Rows.Count > 0)
                {
                    GetMergedAll.DataSource = dt;
                    GetMergedAll.DataBind();
                }
            }
        }
      
    }
    public DataTable GetProfile(string username)
    {
        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        DataSet Dset = new DataSet();

        SqlDataAdapter adapt;
        Dset.Clear();
        adapt = new SqlDataAdapter("GetUserPOSTS", constr);
        adapt.Fill(Dset, "GetUserPOSTS");
        adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
        adapt = new SqlDataAdapter("GetADPOST", constr);
        adapt.Fill(Dset, "GetADPOST");
        adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
        // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
        adapt.SelectCommand.Parameters.AddWithValue("@UserName", username);
        adapt.SelectCommand.Parameters.AddWithValue("@Id", Id);
        DataTable table1 = new DataTable();
        DataTable table2 = new DataTable();

        table1 = Dset.Tables[0];
        table2 = Dset.Tables[1];

        table2.Merge(table1);

        GetMergedAll.DataSource = table2;
        GetMergedAll.DataBind();
        

error

 

Erro

Server Error in '/' Application.
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: CS0161: 'Controls_GetCommunityPost.GetProfile(string)': not all code paths return a value

Source Error:


Line 54:       
Line 55:     }
Line 56:     public DataTable GetProfile(string username)
Line 57:     {
Line 58:         SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);

 

Posted on Nov 10, 2016 03:21 AM
micah says:
public DataTable GetProfile(string username)
    {
        SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
        DataSet Dset = new DataSet();
 
        SqlDataAdapter adapt;
        Dset.Clear();
        adapt = new SqlDataAdapter("GetUserPOSTS", constr);
        adapt.Fill(Dset, "GetUserPOSTS");
        adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
        adapt = new SqlDataAdapter("GetADPOST", constr);
        adapt.Fill(Dset, "GetADPOST");
        adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
        // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
        adapt.SelectCommand.Parameters.AddWithValue("@UserName", username);
        adapt.SelectCommand.Parameters.AddWithValue("@Id", Id);
        DataTable table1 = new DataTable();
        DataTable table2 = new DataTable();
 
        table1 = Dset.Tables[0];
        table2 = Dset.Tables[1];
 
        table2.Merge(table1);
 
        GetMergedAll.DataSource = table2;
        GetMergedAll.DataBind();

 Replace the above method with the below.

public DataTable GetProfile(string username)
{
    SqlConnection constr = new SqlConnection(ConfigurationManager.ConnectionStrings["conn"].ConnectionString);
    DataSet Dset = new DataSet();

    SqlDataAdapter adapt;
    Dset.Clear();
    adapt = new SqlDataAdapter("GetUserPOSTS", constr);
    adapt.Fill(Dset, "GetUserPOSTS");
    adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
    adapt = new SqlDataAdapter("GetADPOST", constr);
    adapt.Fill(Dset, "GetADPOST");
    adapt.SelectCommand.CommandType = CommandType.StoredProcedure;
    // adp.SelectCommand.Parameters.AddWithValue("@Email", username);
    adapt.SelectCommand.Parameters.AddWithValue("@UserName", username);
    adapt.SelectCommand.Parameters.AddWithValue("@Id", Id);
    DataTable table1 = new DataTable();
    DataTable table2 = new DataTable();

    table1 = Dset.Tables[0];
    table2 = Dset.Tables[1];

    table2.Merge(table1);
    return table2;
}