2

Service class

public string[] loadSecretQues(string email)
{
    string[] ques= new string[3];

    dbConn = new OdbcConnection(dbConnString);
    dbConn.Open();
    cmd = new OdbcCommand();
    cmd.Connection = dbConn;

    cmd = new OdbcCommand("SELECT q1, q2, q3 FROM Information WHERE EmailAdd = '" + email + "'", dbConn);
    dRead = cmd.ExecuteReader();

    while (dRead.Read())
    {
        if (dRead.HasRows)
        {
            for (int i = 1; i <= 3; i++)
            {
                for (int j = 0; j <= 3; j++)
                {
                    ques[j] = dRead["q" + i].ToString();
                }
                return ques[i];
            }
        }
    }
}

Page.aspx

protected void btnCheck_Click(object sender, EventArgs e)
{
    cmbQues.Items.Add(srvc.loadSecretQues(txtEmail.Text));
}

Good day guys. I am seeking for help. I want to return array values from a function inside a class. The process is, I want to retrieve 3 questions (which are string data type) from my database and store it in a combobox. Any suggestions how to retrieve those three questions? Any other ways? Thanks in advance! :)

paulooooo
  • 23
  • 1
  • 6
  • Your for i loop will only process the first value 1, as you then return from your function. you need to build up surely an array of arrays and return the whole thing? – BugFinder Aug 11 '15 at 06:57
  • @BugFinder sorry, I don't get what you are trying to say. Can you show me how to do it, would you mind? ") – paulooooo Aug 11 '15 at 07:06

2 Answers2

0

You are returning from an inner loop so that rest of iterations ware cancelled, and also you miss to Dispose the command as well as close the open connection. so i suggest you to dispose the command as well as the connection before return, hence the code will be like the following:

no need to check dvc_mst_tdeposit, because if it has no rows control comes out from while. no need for the outer for Loop(i loop) since (j Loop is enough to handle this scenario.

dRead = cmd.ExecuteReader();
while (dRead.Read())
{
    for (int j = 0; j < 3; j++)
      {
         ques[j] = dRead["q" + i].ToString();
      }              
}
dRead.Close();
cmd.Dispose();
dbConn.Close();
return ques;
sujith karivelil
  • 28,671
  • 6
  • 55
  • 88
  • I have an error in capturing the items into combobox - The best overloaded method match for 'System.Web.UI.WebControls.ListItemCollection.Add(System.Web.UI.WebControls.ListItem)' has some invalid arguments - cannot convert from 'string[]' to 'System.Web.UI.WebControls.ListItem' – paulooooo Aug 11 '15 at 07:20
  • so you have to return List from the function – sujith karivelil Aug 11 '15 at 07:28
0

Use List instead of string array.check the link for advantage

public List<string> loadSecretQues(string email)
{
    List<string> ques=new List<string>();

    dbConn = new OdbcConnection(dbConnString);
    dbConn.Open();
    cmd = new OdbcCommand();
    cmd.Connection = dbConn;

    cmd = new OdbcCommand("SELECT q1, q2, q3 FROM Information WHERE EmailAdd = '" + email + "'", dbConn);
    dRead = cmd.ExecuteReader();

 if (dRead.HasRows)
  {
    while (dRead.Read())
    {
      ques.Add(dRead["yourColumnName"].ToString());
    }
  }
return ques;
}
Community
  • 1
  • 1
shreesha
  • 1,811
  • 2
  • 21
  • 30