I have included a copy of the aspx file and the work behind code file, my problem is I am trying to figure out how to get the text entered in the text boxes so I can update a mysql database. I have tried using the findcontrol but I'm not sure if I am doing this right. As the program creates the textboxes at runtime, I cannot use the textbox control method. Could someone point me in the right direction as to how to do this would be appreciated :)
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using MySql.Data;
using MySql.Data.MySqlClient;
using System.Data;
namespace prfxcon2.prfxcadmin
{
public partial class crtdbasex : System.Web.UI.Page
{
string tsttxb1 = "";
string tstddl1 = "";
string tsttxb2 = "";
protected void Page_Load(object sender, EventArgs e)
{
}
protected void addmorebtn_Click(object sender, EventArgs e)
{
int addm = Convert.ToInt32(addmoretbx.Text);
TextBox[] txtlbl = new TextBox[addm];
DropDownList[] txtnms = new DropDownList[addm];
TextBox[] txtlength = new TextBox[addm];
for (int u = 0; u < txtnms.Count(); u++)
{
txtnms[u] = new DropDownList();
}
for (int y = 0; y < txtlbl.Count(); y++)
{
txtlbl[y] = new TextBox();
}
for (int z = 0; z < txtlength.Count(); z++)
{
txtlength[z] = new TextBox();
}
int i = 0;
int s = 0;
int t = 0;
foreach (TextBox txtlg in txtlength)
{
string tname = "length" + t.ToString();
txtlg.ID = tname;
txtlg.Visible = true;
Panel3.Controls.Add(txtlg);
t++;
}
foreach (TextBox lblx in txtlbl)
{
string xname = "Field" + s.ToString();
lblx.ID = xname;
lblx.Visible = true;
lblx.Text = xname;
Panel1.Controls.Add(lblx);
string zza = lblx.Text + s.ToString();
tsttxb1 = zza;
s++;
}
foreach (DropDownList txt in txtnms)
{
string zname = "crttxtbx" + i.ToString();
txt.ID = zname;
txt.Height = 26;
txt.DataSourceID = "crtdtatypesrc";
txt.DataTextField = "dta_name";
txt.DataValueField = "dta_name";
txt.Visible = true;
Panel2.Controls.Add(txt);
i++;
}
}
protected void crtdbasebtn_Click(object sender, EventArgs e)
{
string sqlconx = "server=192.168.1.25;Uid=spage;Pwd=P1rate3r;database=perfmonx;";
string sqlcmdx = "INSERT INTO `database`(`adh_dbname`,`adh_fields`,`adh_datatype`,`adh_length`) VALUES (@dbname,@dbfield,@dbdatatype,@dblength)";
int a = 0;
int txtll = Convert.ToInt32(addmoretbx.Text);
TextBox[] tbxc = new TextBox[txtll];
for (int w = 0; w < tbxc.Count(); w++)
{
tbxc[w] = new TextBox();
}
foreach (TextBox sft in tbxc)
{
Control fctrl = FindControl("crtfield" + a);
string fctrlx = Convert.ToString(fctrl);
Control fctrl1 = FindControl("Field" + a);
string fctrly = Convert.ToString(fctrl1);
Control fctrl2 = FindControl("length"+a);
string fctrlz = Convert.ToString(fctrl2);
MySqlConnection sqlcon = new MySqlConnection(sqlconx);
sqlcon.Open();
try
{
MySqlCommand sqlcmd = new MySqlCommand(sqlcmdx, sqlcon);
sqlcmd.Parameters.AddWithValue("@dbname", crtdbasename.Text);
sqlcmd.Parameters.AddWithValue("@dbfield", fctrlx + ".Text");
sqlcmd.Parameters.AddWithValue("dbdatatype", fctrly + ".SelectedValue");
sqlcmd.Parameters.AddWithValue("dblength", fctrlz + ".Text");
sqlcmd.ExecuteNonQuery();
}
catch (MySqlException ex)
{
throw (ex);
}
finally
{
if (sqlcon.State == ConnectionState.Open)
{
sqlcon.Close();
}
}
}
}
}
}
<%@ Page Title="" Language="C#" MasterPageFile="~/prfxcadmin/prfxcadmin.master" AutoEventWireup="true" CodeBehind="crtdbasex.aspx.cs" Inherits="prfxcon2.prfxcadmin.crtdbasex" %>
<asp:Content ID="HeaderContent" ContentPlaceHolderID="HeadContent" runat="server">
</asp:Content>
<asp:Content ID="BodyContent" ContentPlaceHolderID="MainContent" runat="server">
<h2>Create Table Admin</h2>
<table>
<tr>
<td>
<asp:Label ID="crtdbasenamelbl" runat="server" AssociatedControlID="crtdbasename">Table Name:</asp:Label>
</td>
<td>
<asp:TextBox ID="crtdbasename" CssClass="textEntry" runat="server"></asp:TextBox>
<asp:RequiredFieldValidator ID="crtdbasenamereq" runat="server" ControlToValidate="crtdbasename"
CssClass="failureNotification" ErrorMessage="Table name is required." ToolTip="Table name is required."
ValidationGroup="crtdbasevg">!</asp:RequiredFieldValidator>
</td>
</tr>
</table>
<asp:SqlDataSource ID="crtdtatypesrc" runat="server"
ConnectionString="<%$ ConnectionStrings:prfxconn %>"
ProviderName="<%$ ConnectionStrings:prfxconn.ProviderName %>"
SelectCommand="SELECT `dta_name` FROM `dtatype`">
</asp:SqlDataSource>
<asp:TextBox ID="addmoretbx" runat="server" CssClass="textEntry" Width="30px"></asp:TextBox>
<asp:Button ID="addmorebtn" runat="server" CssClass="submitButton" Text="Add Fields"
onclick="addmorebtn_Click" />
<table>
<tr>
<td align="center">
<strong>Field Name</strong>
</td>
<td>
<strong>Data Type</strong>
</td>
<td>
<strong>Length</strong>
</td>
</tr>
<tr>
<td style="width:50px">
<asp:Panel ID="Panel1" runat="server">
</asp:Panel>
</td>
<td style="width:100px">
<asp:Panel ID="Panel2" runat="server">
</asp:Panel>
</td>
<td style="width:100px">
<asp:Panel ID="Panel3" runat="server">
</asp:Panel>
</td>
</tr>
</table>
<table width="98%">
<tr align="center">
<td>
<asp:Button ID="crtdbasebtn" runat="server" CssClass="textEntry" Width="200px"
Text="Create Table" onclick="crtdbasebtn_Click" />
</td>
</tr>
</table>
</asp:Content>