Click here to Skip to main content
14,927,528 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi I have function like this.
I mentioned the problem in below function with(//lines)
C#
public DataTable GetSubMainTypes(int mainid)
    {
        System.Data.DataTable dt = new DataTable();
        if (ConnectionString.Length > 0)
        {
            System.Data.SqlClient.SqlConnection dbConn = null;
            try
            {
                if (!runmethod)
                {
                    string Query = "";
                    if (mainid == 1)
                    {
                        Query = "SELECT dbo.submaintypes.submainid, dbo.SubMainTypes.Name, dbo.SubMainTypes.Type FROM dbo.MainTypes INNER JOIN dbo.SubMainTypes ON dbo.MainTypes.MainID = dbo.SubMainTypes.MainID where dbo.MainTypes.MainID =" + mainid.ToString();
                    }
                    else if (mainid == 2)
                    {
                        Query = "select c.name as 'Name',c.city as 'City',c.state as 'State',c.telephone as 'Telephone',c.organization as 'Organization',c.address1 as 'Address1',c.address2 as 'Address2',c.postalcode as 'PostalCode',cc.name as 'Country',c.telephone as 'Telephone',c.website as 'Website',c.rating as 'Rating',c.created as 'Created',c.createdby as 'CreadtedBy',c.lastmodified as 'LastModified',w.name  as 'WorkFlow' from customers c inner join countries cc on c.countryid=cc.id inner join workflow w  on w.wid=c.wid";
                    }
                    else if (mainid == 3)
                    {
                        Query = "select pt.ID as 'ID',pt.name as 'Name',T.name as 'Type' from ProjectTypes pt inner join Types t on pt.Typeid=t.Typeid";
                    }
                    else if (mainid == 4)
                    {
                        Query = "select p.projid, p.Name as 'Name',t.Name as 'Type' from project p inner join types t on p.typeid=t.typeid";
                    }
                    else if (mainid == 5)
                    {
                        FillGridview("select AID,[Name] as 'Name',DeadLine,ADescription from Assignment where Createdby= " + "'" + Environment.UserName + "'");
//The above function Returns DataSet
//I dont want to execute the below lines if (mainid=5)
                    }
                    dbConn = new System.Data.SqlClient.SqlConnection(ConnectionString);
                    dbConn.Open();
                    System.Data.SqlClient.SqlCommand dbCmd = new System.Data.SqlClient.SqlCommand(Query, dbConn);
                    System.Data.SqlClient.SqlDataAdapter da = new System.Data.SqlClient.SqlDataAdapter(dbCmd);
                    da.Fill(dt);
                    da.Dispose(); dbCmd.Dispose();
                    return dt;
                }
            }
            catch { return dt; }
            finally
            {
                if (dbConn.State.ToString().Equals("Open"))
                {
                    dbConn.Close();
                }
                dbConn.Dispose();
            }
        }
        else { return dt; }
    }
Posted
Updated 6-Mar-10 0:31am
v2

Add a boolean variable indicating that the query is to be run, and use a switch statement instead of if/else.

bool executequery = true;
DataTable dt = null;
string Query = "";
switch (mainid)
{
    case 1 :
        Query = "blah blah";
        break;

    case 2 :
        Query = "blah blah blah";
        break;

    ...

    case 5 :
        FillGridView(...);
        executequery = false;
        break;
    
    default :
        executequery = false;
        break;
}
if (executequery)
{
    dt = new DataTable();
    dbConn = new...
    ...
}


The reason I changed the datatable initialization is because if you're not going to perform a query, there's no point in allocating the datatable. If your method returns null, you know there's nothing to do.
   
v4
Method 'FillGridview' might be filling some dataset but is not returning anything. You should fill this in a dataset if you want to stop execution after this method.

In case you dont want to fill it and dont want to execute anything after this line then just write "return dummyDataTable;"

create dummyDatatable object of type DataTable that has nothing.


OPTION2:
Use switch case instead of If and use break;
   
v2
It gives me an error


Not all code paths returns a value(when i use Switch case)
DataGridview with Empty Datatable(when i use If else).

Any Help..
   

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