Reputation: 83
For reference click the image below image
Want to load the page with database values one by one like in the below image
if add some text int the text box it will add to the database when page reloads it will show with entered value
please help to find.
if you want my code means i will give
NpgsqlConnection conn = new NpgsqlConnection`enter code here`("Server=192.166.0.121;User Id=trh; " + "Password=trh;Database=checking_DB;"); //+ "Pooling=true;MaxPoolSize=100;Timeout=20;"
conn.Open();
NpgsqlCommand command = new NpgsqlCommand("select Id,Name from tbl_Names", conn);
// labelSno.Text = command.ExecuteNonQuery().ToString();
lblSno.Text = command.ExecuteScalar().ToString();
lblProfitCenter.Text = command.ExecuteScalar().ToString();
<div>
<table id="tbl1">
<tr>
<th> S.No</th>
<th> Name</th>
<th> Type</th>
</tr>
<tr>
<td><asp:Label runat="server" ID="lblSno"></asp:Label></td>
<td><asp:Label runat="server" ID="lblProfitCenter"></asp:Label></td>
<td> <asp:Button runat="server" ID="btnEdit" Text="Edit" />
<asp:Button runat="server" ID="btnDelete" Text="Delete" />
</td>
</tr>
<tr>
<td></td>
<td><asp:TextBox runat="server" ID="txtProfitCenter"></asp:TextBox></td>
<td><asp:Button runat="server" ID="btnAdd" Text="Add" OnClick="btnAdd_Click" /></td>
</tr>
</table>
</div>
Upvotes: 1
Views: 95
Reputation: 545
You can store your database value in a DataTable object and add label object dinamically in your page.
I will assume you have a Panel component in your page to add the labels programmatically ok?
For example:
NpgsqlConnection conn = new NpgsqlConnection("Server=192.166.0.121;User Id=trh; " + "Password=trh;Database=checking_DB;");
conn.Open();
DataTable dt = new DataTable();
string command = "select Id,Name from tbl_Names";
using (NpgsqlDataAdapter Adpt = new NpgsqlDataAdapter(command, conn))
{
Adpt.Fill(dt);
foreach(DataRow row in dt.Rows)
{
Label lblSno = new Label();
Label lblProfitCenter = new Label();
lblSno.Text = row[0].ToString();
lblProfitCenter.Text = row[1].ToString();
Panel1.Controls.Add(lblSno);
Panel1.Controls.Add(lblProfitCenter);
}
}
Upvotes: 1
Reputation: 756
I will give you the overview. You just follow it, hope you will get the expected solution.
Panel pnlTextBox;
protected void Page_Load(object sender, EventArgs e)
{
Literal lt;
Label lb;
Button btnAddTxt = new Button();
btnAddTxt.ID = "btnAddTxt";
btnAddTxt.Text = "Add TextBox";
btnAddTxt.Click += new System.EventHandler(btnAdd_Click);
this.form1.Controls.Add(btnAddTxt);
if (IsPostBack)
{
RecreateControls("txtDynamic", "TextBox");
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
Button btn = (Button)sender;
if (btn.ID == "btnAddTxt")
{
// Can do database stuff here
int cnt = FindOccurence("txtDynamic");
TextBox txt = new TextBox();
txt.ID = "txtDynamic-" + Convert.ToString(cnt + 1);
pnlTextBox.Controls.Add(txt);
Literal lt = new Literal();
lt.Text = "<br />";
pnlTextBox.Controls.Add(lt);
}
}
private int FindOccurence(string substr)
{
string reqstr = Request.Form.ToString();
return ((reqstr.Length - reqstr.Replace(substr, "").Length) / substr.Length);
}
private void RecreateControls(string ctrlPrefix, string ctrlType)
{
string[] ctrls = Request.Form.ToString().Split('&');
int cnt = FindOccurence(ctrlPrefix);
if (cnt > 0)
{
Literal lt;
for (int k = 1; k <= cnt; k++)
{
for (int i = 0; i < ctrls.Length; i++)
{
if (ctrls[i].Contains(ctrlPrefix + "-" + k.ToString()))
{
string ctrlName = ctrls[i].Split('=')[0];
string ctrlValue = ctrls[i].Split('=')[1];
//Decode the Value
ctrlValue = Server.UrlDecode(ctrlValue);
if (ctrlType == "TextBox")
{
TextBox txt = new TextBox();
txt.ID = ctrlName;
txt.Text = ctrlValue;
pnlTextBox.Controls.Add(txt);
lt = new Literal();
lt.Text = "<br />";
pnlTextBox.Controls.Add(lt);
}
break;
}
}
}
}
}
Upvotes: 0