Hi irshad1231,
Refer below sample and change the saving code as per your database structure.
HTML
<table border="0" cellpadding="0" cellspacing="0">
<tr>
<td>Question:</td>
<td><asp:TextBox runat="server" ID="txtQuestion" /></td>
</tr>
<tr>
<td>No Of Option:</td>
<td><asp:TextBox runat="server" ID="txtOptions" AutoPostBack="true" OnTextChanged="GenerateOption" /></td>
</tr>
<tr>
<td colspan="2">
<asp:Panel ID="pnlOptions" runat="server" Visible="false">
</asp:Panel>
</td>
</tr>
<tr>
<td>Answer:</td>
<td><asp:TextBox runat="server" ID="txtAnswer" /></td>
</tr>
<tr>
<td colspan="2" align="center">
<asp:Button Text="Save" runat="server" OnClick="Save" />
</td>
</tr>
</table>
Namespaces
C#
using System.Configuration;
using System.Data.SqlClient;
VB.Net
Imports System.Configuration
Imports System.Data.SqlClient
Code
C#
protected void Page_PreInit(object sender, EventArgs e)
{
List<string> keys = Request.Form.AllKeys.Where(key => key.Contains("txtDynamic")).ToList();
int i = 1;
foreach (string key in keys)
{
this.CreateTextBox("txtDynamic" + i);
i++;
}
}
protected void GenerateOption(object sender, EventArgs e)
{
pnlOptions.Controls.Clear();
int index = pnlOptions.Controls.OfType<TextBox>().ToList().Count + 1;
for (int i = 0; i < Convert.ToInt32(txtOptions.Text); i++)
{
this.CreateTextBox("txtDynamic" + (index + i));
}
pnlOptions.Visible = true;
}
protected void Save(object sender, EventArgs e)
{
string question = txtQuestion.Text.Trim();
string answer = txtAnswer.Text.Trim();
string query = "INSERT INTO QuizAnswer (Question,Answer,";
for (int i = 1; i <= Convert.ToInt32(txtOptions.Text); i++)
{
query += "Option" + i + ",";
}
query = query.Remove(query.Length - 1, 1);
query += ") VALUES(@Question,@Answer,";
for (int i = 1; i <= Convert.ToInt32(txtOptions.Text); i++)
{
query += "@Option" + i + ",";
}
query = query.Remove(query.Length - 1, 1) + ")";
string constr = ConfigurationManager.ConnectionStrings["constr"].ConnectionString;
using (SqlConnection con = new SqlConnection(constr))
{
using (SqlCommand cmd = new SqlCommand(query))
{
cmd.Parameters.AddWithValue("@Question", question);
cmd.Parameters.AddWithValue("@Answer", answer);
int i = 1;
foreach (TextBox textBox in pnlOptions.Controls.OfType<TextBox>())
{
cmd.Parameters.AddWithValue("@Option" + 1, textBox.Text);
i++;
}
cmd.Connection = con;
con.Open();
cmd.ExecuteNonQuery();
con.Close();
}
}
}
private void CreateTextBox(string id)
{
Label lbl = new Label();
lbl.Text = "Option " + id.Replace("txtDynamic", "") + ": ";
pnlOptions.Controls.Add(lbl);
Literal lt = new Literal();
lt.Text = " ";
pnlOptions.Controls.Add(lt);
TextBox txt = new TextBox();
txt.ID = id;
pnlOptions.Controls.Add(txt);
lt = new Literal();
lt.Text = "<br />";
pnlOptions.Controls.Add(lt);
}
VB.Net
Protected Sub Page_PreInit(ByVal sender As Object, ByVal e As EventArgs) Handles Me.PreInit
Dim keys As List(Of String) = Request.Form.AllKeys.Where(Function(key) key.Contains("txtDynamic")).ToList()
Dim i As Integer = 1
For Each key As String In keys
Me.CreateTextBox("txtDynamic" & i)
i += 1
Next
End Sub
Protected Sub GenerateOption(ByVal sender As Object, ByVal e As EventArgs)
pnlOptions.Controls.Clear()
Dim index As Integer = pnlOptions.Controls.OfType(Of TextBox)().ToList().Count + 1
For i As Integer = 0 To Convert.ToInt32(txtOptions.Text) - 1
Me.CreateTextBox("txtDynamic" & (index + i))
Next
pnlOptions.Visible = True
End Sub
Protected Sub Save(ByVal sender As Object, ByVal e As EventArgs)
Dim question As String = txtQuestion.Text.Trim()
Dim answer As String = txtAnswer.Text.Trim()
Dim query As String = "INSERT INTO QuizAnswer (Question,Answer,"
For i As Integer = 1 To Convert.ToInt32(txtOptions.Text)
query += "Option" & i & ","
Next
query = query.Remove(query.Length - 1, 1)
query += ") VALUES(@Question,@Answer,"
For i As Integer = 1 To Convert.ToInt32(txtOptions.Text)
query += "@Option" & i & ","
Next
query = query.Remove(query.Length - 1, 1) & ")"
Dim constr As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
Using con As SqlConnection = New SqlConnection(constr)
Using cmd As SqlCommand = New SqlCommand(query)
cmd.Parameters.AddWithValue("@Question", question)
cmd.Parameters.AddWithValue("@Answer", answer)
Dim i As Integer = 1
For Each textBox As TextBox In pnlOptions.Controls.OfType(Of TextBox)()
cmd.Parameters.AddWithValue("@Option" & 1, textBox.Text)
i += 1
Next
cmd.Connection = con
con.Open()
cmd.ExecuteNonQuery()
con.Close()
End Using
End Using
End Sub
Private Sub CreateTextBox(ByVal id As String)
Dim lbl As Label = New Label()
lbl.Text = "Option " & id.Replace("txtDynamic", "") & ": "
pnlOptions.Controls.Add(lbl)
Dim lt As Literal = New Literal()
lt.Text = " "
pnlOptions.Controls.Add(lt)
Dim txt As TextBox = New TextBox()
txt.ID = id
pnlOptions.Controls.Add(txt)
lt = New Literal()
lt.Text = "<br />"
pnlOptions.Controls.Add(lt)
End Sub