none
? Not all code path return a value.... RRS feed

  • Question

  • here is the code which i have written...

    public

    class DBFunctions

    {

     

    public static String GetmaxId(string tblname,string fldname,System.Data.SqlClient.SqlConnection conn)

    {

     

    string sql;

     

    string maxId;

     

    System.Data.SqlClient.

    SqlCommand sqlcmd;

    System.Data.SqlClient.

    SqlDataReader sqlRead;

    sql =

    " select " + fldname;

    sql = sql +

    " from " + tblname;

    sqlcmd =

    new System.Data.SqlClient.SqlCommand(sql, conn);

    sqlRead = sqlcmd.ExecuteReader();

     

    while (sqlRead.Read())

    {

    maxId = sqlRead[fldname].ToString();

     

    return(maxId);

    }

     

    sqlRead.Close ();

     

    }

     

    }

    please if any one can help....to it

    Wednesday, August 12, 2009 6:22 AM

Answers

  • Hello,ChetanRg

    I have two advice for u:
    1,if you wanna get the max value of a column,you may try to use SQL Max function,if the data value is numeric
    for example,"select max(ID) from [myTable]"

    2,in your code:

    while (sqlRead.Read())

     

    return(maxId);

    }

    only when sqlRead.Read()==true this function contains a return value,so you will get that exception
    you may change ir like this

    maxId=string.Empty;

    while (sqlRead.Read()) return(maxId);

    {

    maxId = sqlRead[fldname].ToString();

    }

    {

    maxId = sqlRead[fldname].ToString();

     


    Wenn ich dich hab’,gibt es nichts, was unerträglich ist.坚持不懈!http://hi.baidu.com/1987raymond
    • Marked as answer by Yichun_Feng Tuesday, August 18, 2009 1:27 AM
    Wednesday, August 12, 2009 6:41 AM