locked
wcf return values RRS feed

  • Question

  • User736276958 posted

    hi all

    i am using wcf in my project

    i am facing one issue in wcf

    i am writing a function in wcf , which will return dataset

    in the same function i am writing 2 different query statement

    ie iam getting one value from one table and passing that value in another query statement and iam returning dataset

    but iam getting error with first query statement if there is no value in the database as "there is no row at position 0"

    my code is

    public DataSet GetDetails(String empname)
    {

    Connect();
    string query = String.Format("select empcode from employee where employeename="+ empname +"");
    cmd = new sqlCommand(query, conn);
    dataReader = cmd.ExecuteReader();
    dt.Load(dataReader);----------------------------------------if dt is empty, it is displaying error as "there is no row at position 0".How to check dt, whether it is null or not
    dr = dt.Rows[0];
    ecode = dt.Rows[0].ItemArray[0].ToString();
    cmd = sqlCommand("select * from empdetails where empcode=@ecode", conn);
    cmd.Parameters.AddWithValue("@ecode", ecode);
    sqlDataAdapter sda = new sqlDataAdapter(cmd);
    sda.Fill(ds);
    cmd.ExecuteNonQuery();
    conn.Close();

    return ds;
    }

    Tuesday, August 4, 2015 6:04 AM

All replies

  • User61956409 posted

    Hi rishe28,

    dt.Load(dataReader);----------------------------------------if dt is empty, it is displaying error as "there is no row at position 0".How to check dt, whether it is null or not

    You could count the rows of datatable to check if it contains any rows.

    if (dt.Rows.Count > 0)
    {
        //it is not null 
    }
    else
    {
        //it is null 
    } 
    

    Best Regards,

    Fei Han

    Wednesday, August 5, 2015 1:32 AM
  • User736276958 posted
    if (dt.Rows.Count > 0)
    {
        //here i will write my code
    }
    else
    {
        //it is null 
    //if it is null, then how can i show error message through wcf and finally i have to return dataset }
    Wednesday, August 5, 2015 5:33 AM
  • User-1926401737 posted

    Hi rishe28,

    Your problem is related to WCF, you could ask for help on these WCF forums.

    With Regards,

    swing

    Tuesday, August 11, 2015 5:37 AM
  • User1086429607 posted

    This could be tricky as you could have many ways of accomplishing this. One way you could go about this is have another String method and call that instead thus encapsulating the original method.

    public string GetDataSetResult(string empname)
    {
      Dataset tempDs = GetDetails(String empname);
    
      if (tempDs.Count() < 0)
        {
          return "No data found";
        }
    }

    Ive not checked for null so you may need to tweak the code a little if this is the approach you would like to take. You could even make it into another separate method and call it first. The problem you will have is having too many calls to the database in this manner unless you use Complex types and code it to work accordingly 

    Tuesday, August 11, 2015 11:28 AM