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! :)

2
  • 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? Commented Aug 11, 2015 at 6:57
  • @BugFinder sorry, I don't get what you are trying to say. Can you show me how to do it, would you mind? ") Commented Aug 11, 2015 at 7:06

2 Answers 2

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;
Sign up to request clarification or add additional context in comments.

2 Comments

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'
so you have to return List<ComboBoxItems> from the function
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;
}

1 Comment

Oh..! List<string> can directly converted to System.Web.UI.WebControls.ListItem

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.