开发者

what is the use of hashtable? asp.net c#

Hi What is the use of hashtable? can i use to move record from database in click event of the button using hashtable? if yes then how possible? Thank You.

//This is the function i used to get next record from database
public void getNextQuestion()
    {
        if (Convert.ToInt32(Session["counter"].ToString()) < 7)
        {
            if (RblOption.SelectedIndex >= 0)
            {
                string str1 = "";
                SqlDataAdapter da3 = new SqlDataAdapter(str1, sqlconn);

                Session["selans"] = RblOption.SelectedIndex.ToString();
                if (Session["Answer"].ToString() == RblOption.SelectedIndex.ToString())
                {
                    int score = Convert.ToInt32(txtScore.Text) + 1;
                    txtScore.Text = score.ToString();
                    lblScore.Text = "Score : " + Convert.ToString(score);
                }

                //Random rnd = new Random();
                //var i = rnd.Next(1, 6);

                SqlDataAdapter da = new SqlDataAdapter("select Q_id from Test_Final where Serial='1' order by Q_id", sqlconn);
                DataSet ds = new DataSet();
                da.Fill(ds, "Test_Final");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    myArray = new int[ds.Tables[0].Rows.Count];
                    bool flag;
                    for (int i = 1; i < ds.Tables[0].Rows.Count; i++)
                    {
                        myArray[i] = Convert.ToInt32(ds.Tables[0].Rows[i]["Q_id"].ToString());
                        //Response.Write(myArray[i]);
                        Random random = new Random();

                        int randomNo = random.Next(0, myArray.Length);

                        flag = true;

                        for (int x = 0; x < myArray.Length; x++)
                        {

                            if (randomNo == myArray[x])
                            {

                                i--;

                                flag = false;

                                break;

                            }

                        }
                        if (flag)

                            myArray[i] = randomNo;

                    }



                    Session["counter"] = Convert.T开发者_开发知识库oString(Convert.ToInt32(Session["counter"].ToString()) + 1);

                    getQuestion(myArray[z]);

                }
                else
                {
                    RegisterStartupScript("myAlert", "<script>alert('Please select the option')</script>");

                }

            }

            else
            {
                Response.Redirect("Default.aspx");
            }

        }
    }


Add code in button click,as below

protected void Button1_Click(object sender, EventArgs e) { int key; string name = string.Empty; string str = string.Empty; Hashtable hashtable = new Hashtable(); key = 1; name = "Jake"; hashtable.Add(key, name); key = 2; name = "peter"; hashtable.Add(key, name); key = 3; name = "lily"; hashtable.Add(key, name); foreach (DictionaryEntry de in hashtable) { str = str + "key=" + de.Key + " " + " value=" + de.Value.ToString() + "
"; } }

If I am getting you corretly then you want something like this

  public Hashtable Createashtable(string keyColumnName, DataTable table)
    {
        Hashtable hash = new Hashtable();

        for (int rowIndex = 0; rowIndex < table.Rows.Count; rowIndex++)
        {
            hash.Add(table.Rows[rowIndex][keyColumnName], table.Rows[rowIndex]);
        }

        return hash;
    }
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜