Dynamic table with dynamic controls and mySql ...

Joined
Jun 17, 2011
Messages
1
Reaction score
0
Hi, I need some help to get this to work.

At first I had problems with the dynamic events not firing, now the problem is that the select from mySql returns data before it's updated.

If someone would please look at the code.. been rewriting this code like 10 times over the few weeks :(
(mainly because I had problems with the event of the dynamic controls, so I tried alot of different approaches)

Code:
namespace KGen
{
    public partial class kkwithDB : System.Web.UI.Page
    {
        OdbcConnection sqlCon = new OdbcConnection("DSN=demoserver");
        protected DataSet sqlDs(string queryString)
        {
            OdbcDataAdapter sqlDa = new OdbcDataAdapter(queryString, sqlCon);
            DataSet sqlDs = new DataSet();
            sqlDa.Fill(sqlDs);
            return sqlDs;
        }
        protected void renderTable(string namn)
        {
            if (namn != "")
            {
                if (sqlDs("select * from kundkort where namn='" + namn + "'") == null)
                {
                    sqlDs("insert into kundkort(namn) values('" + namn + "')");
                    sqlDs("insert into kundkortdata(kundKortId,typ) values('" + namn + "','addRow')");
                }
                else
                {
                    Table newTable = new Table();
                    DataSet kundkortRader = sqlDs("select * from kundkort where namn='" + namn + "'");
                    DataSet kundKortDS = sqlDs("select * from kundkortdata where kundKortId='" + namn + "'");
                    DataRow dRr = kundkortRader.Tables[0].Rows[0];
                    testBox.Text = dRr[0] + " " + dRr[1] + " "+dRr[2] + " "+dRr[3];
                    string rader = dRr[2].ToString();
                    string kolumner = dRr[3].ToString();
                    for (int i = 0; i < Convert.ToInt32(rader); i++)
                    {
                        TableRow tR = new TableRow();
                        for (int j = 0; j < Convert.ToInt32(kolumner); j++)
                        {
                            DataSet dS = sqlDs("select * from kundkortdata where kundKortId='" + namn + "' and rad='" + i + "' and cell='" + j + "'");
                            if (dS.Tables[0].Rows.Count > 0)
                            {
                                DataRow dR = dS.Tables[0].Rows[0];
                                TableCell tC = createCell(dR[2].ToString(), "",i.ToString()+j.ToString());
                                tR.Cells.Add(tC);
                            }
                            else
                            {
                                TableCell tC = createCell("", "", i.ToString() + j.ToString());
                                tR.Cells.Add(tC);
                            }
                        }
                        
                        /*foreach (DataRow dR in kundKortDS.Tables[0].Rows)
                        {
                            //läs in typ av kontroll och skapa den på rätt plats
                            string type = dR[2].ToString();
                            int row = Convert.ToInt32(dR[3].ToString());
                            int column = Convert.ToInt32(dR[4].ToString());
                            string data = dR[5].ToString();
                            TableCell tC = createCell(type, row, column, data);
                            tR.Cells.AddAt(column,tC);
                        }*/
                        newTable.Rows.Add(tR);
                    }
                    TableRow addRow = new TableRow();
                    TableCell addRowBtn = createCell("addRow", "",Convert.ToInt32(rader+1).ToString());
                    addRow.Cells.Add(addRowBtn);
                    newTable.Rows.Add(addRow);
                    dynTableHolder.Controls.Add(newTable);
                }
            }
        }
        protected void Page_Load(object sender, EventArgs e)
        {
            string namn = Page.Request["namn"];
            renderTable(namn);
        }
        protected TableCell createCell(string type,string data,string id)
        {
            switch (type)
            {
                case "addRow":
                    TableCell addRowTc = new TableCell();
                    Button addButton = new Button();
                    addButton.CommandArgument = "1";
                    addButton.ID = "addBtn_"+id;
                    addButton.Click+=new EventHandler(addButton_Click);
                    addButton.Text = "Ny rad";
                    addRowTc.Controls.Add(addButton);
                    return addRowTc;
                case "dropDown":
                    TableCell dropDownCell = new TableCell();
                    DropDownList ControlDropDown = new DropDownList();
                    ControlDropDown.Items.Add("test");
                    dropDownCell.Controls.Add(ControlDropDown);
                    return dropDownCell;
                default:
                    TableCell emptyCell = new TableCell();
                    DropDownList newControlDropDown = new DropDownList();
                    newControlDropDown.Items.Add("textBox");
                    newControlDropDown.Items.Add("dropDown");
                    newControlDropDown.Items.Add("radioButton");
                    newControlDropDown.Items.Add("other");
                    newControlDropDown.AutoPostBack = true;
                    newControlDropDown.SelectedIndexChanged+=new EventHandler(newControlDropDown_SelectedIndexChanged);
                    emptyCell.Controls.Add(newControlDropDown);
                    return emptyCell;
            }
            
        }
        protected void newControlDropDown_SelectedIndexChanged(object sender, EventArgs e)
        {

        }
        protected void addButton_Click(object sender, EventArgs e)
        {
            
            Response.Write("add_event handled");
            string namn = Page.Request["namn"];
            sqlDs("update kundkort set rader=rader+1 where namn='"+namn+"'");
            //System.Threading.Thread.Sleep(3000);
            
        }
        protected void RadioButtonList1_SelectedIndexChanged(object sender, EventArgs e)
        {
            Response.Write("radio_event handled "+RadioButtonList1.SelectedValue);
            string namn = Page.Request["namn"];
            sqlDs("update kundkort set kolumner='" + RadioButtonList1.SelectedValue + "' where namn='" + namn + "'");
            //System.Threading.Thread.Sleep(3000);
        }
    }
}

Thanks!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top