0
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Text;
using System.Web.Services;
using System.IO;

namespace T_Smade
{

    public partial class ConferenceManagement : System.Web.UI.Page
    {
        volatile int i = 0;

        protected void Page_Load(object sender, EventArgs e)
        {
            GetSessionList();
        }


        public void GetSessionList()
        {
            string secondResult = "";
            string userName = "";

            try
            {
                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    userName = HttpContext.Current.User.Identity.Name;
                }

                SqlConnection thisConnection = new SqlConnection(@"data Source=ZOLA-PC;AttachDbFilename=D:\2\5.Devp\my DB\ASPNETDB.MDF;Integrated Security=True");
                thisConnection.Open();

                SqlCommand secondCommand = thisConnection.CreateCommand();
                secondCommand.CommandText = "SELECT myApp_Session.session_id FROM myApp_Session, myApp_Role_in_Session where myApp_Role_in_Session.user_name='" + userName + "' and myApp_Role_in_Session.session_id=myApp_Session.session_id";
                SqlDataReader secondReader = secondCommand.ExecuteReader();

                while (secondReader.Read())
                {
                    secondResult = secondResult + secondReader["session_id"].ToString() + ";";
                }
                secondReader.Close();

                SqlCommand thisCommand = thisConnection.CreateCommand();
                thisCommand.CommandText = "SELECT * FROM myApp_Session;";
                SqlDataReader thisReader = thisCommand.ExecuteReader();

                while (thisReader.Read())
                {
                    test.Controls.Add(GetLabel(thisReader["session_id"].ToString(), thisReader["session_name"].ToString()));
                    string[] compare = secondResult.Split(';');
                    foreach (string word in compare)
                    {
                        if (word == thisReader["session_id"].ToString())
                        {
                            test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));
                        }
                    }
                }
                thisReader.Close();
                thisConnection.Close();

            }
            catch (SqlException ex)
            {

            }
        }

        private Button GetButton(string id, string name)
        {
            Button b = new Button();
            b.Text = name;
            b.ID = "Button_" + id + i;
            b.Command += new CommandEventHandler(Button_Click);
            b.CommandArgument = id;
            i++;
            return b;
        }

        private Label GetLabel(string id, string name)
        {
            Label tb = new Label();
            tb.Text = name;
            tb.ID = id;
            return tb;
        }

        protected void Button_Click(object sender, CommandEventArgs e)
        {
            Response.Redirect("EnterSession.aspx?session=" + e.CommandArgument.ToString());
        }
    }

when users click from this page the next page is generated as

www.mypage/Entersession.aspx?session=session_id

but i'd rather to have it like

www.mypage/Entersession.aspx?session=session_name

both session_id and session_name are from the database

any idea?

}
1
  • So you want to change your CommandArgument? Commented Apr 6, 2012 at 16:57

3 Answers 3

1

Just change

test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));

to

test.Controls.Add(GetButton(thisReader["session_name"].ToString(), "Join Session"));
Sign up to request clarification or add additional context in comments.

Comments

1

The change you're looking for takes place in where you use your GetButton method

Change:

test.Controls.Add(GetButton(thisReader["session_id"].ToString(), "Join Session"));

To:

test.Controls.Add(GetButton(thisReader["session_name"].ToString(), "Join Session"));

Your first input parameter to GetButton is being mapped to the CommandArgument. So passing session_name in rather than session_id will do the trick.

Comments

1

Is this what you are looking for? Changing CommandArgument to name? It's a very, very easy fix.

Update

You could add a commandArgument parameter to GetButton().

    private Button GetButton(string id, string name, string commandArgument)
    {
        Button b = new Button();
        b.Text = name;
        b.ID = "Button_" + id + i;
        b.Command += new CommandEventHandler(Button_Click);
        b.CommandArgument = commandArgument; // this changed to commandArgument
        i++;
        return b;
    }

    GetButton(thisReader["session_id"].ToString(), "Join Session", thisReader["session_name"].ToString())

3 Comments

This will result "Join Session" always being the CommandArgument.
No over load for method get button takes 2 arguments. this what i got as error
Right, you need to add a third argument as thisReader["session_name"].ToString() as show in the last line of my answer.

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.