It suceesful in first time you enter number .But if remove that umber again enter it is adding .
Like first input 4 in textbox and enter .remove that number textbox input 6 and enter .
it "adding 4+6" 4 alright plus new 6 more will display means totally 10 .
using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
public partial class assigned: System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (IsPostBack && DDLCount > 0)
{
CreateDropDownLists();
}
}
public int DDLCount
{
get
{
// Try to get an instance of the DDLCount ViewState object
object temp = ViewState["DDLCount"];
// If temp is not null then cast it to int and return it,
// otherwise return 0
return temp == null ? 0 : (int)temp;
}
set { ViewState["DDLCount"] = value; }
}
private void CreateDropDownLists()
{
int a = Convert.ToInt32(TextBox4.Text);
// This is just to set the count to a default value
// You may possibly need to generate the count of dropdownlists
// in some other manner, depending on your project
//if (DDLCount == 0)
DDLCount = a; //i have to ask HOw DDLCount is 0
// I can not understand it
for (int i = 0; i < DDLCount; i++)
{
// Create the dropdownlists
DropDownList ddl = new DropDownList();
DropDownList dd2 = new DropDownList();
ddl.ID = "Text" + i;
dd2.ID = "Texts" + i;
LiteralControl l1 = new LiteralControl("<br></br>");
//ddl.Items.Add(new ListItem("TestText_" + i, "TestValue_" + i));
//ddl.Items.Add("anant");
//ddl.Items.Add("pratibha");
//ddl.Items.Add("rahul");
SqlConnection con1 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
con1.Open();
string st = "select doe_name from Doe_detail ";
SqlCommand cmd = new SqlCommand(st, con1);
SqlDataReader dr = cmd.ExecuteReader();
while (dr.Read())
{
ddl.Items.Add(dr.GetString(0).ToString());
}
con1.Close();
SqlConnection con2 = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
con2.Open();
string std = "select phone from Doe_detail ";
SqlCommand cmd1 = new SqlCommand(std, con2);
SqlDataReader dr1 = cmd1.ExecuteReader();
while (dr1.Read())
{
dd2.Items.Add(dr1.GetString(0).ToString());
}
con2.Close();
// Add it to the panel
Panel3.Controls.Add(ddl);
Panel3.Controls.Add(dd2);
}
}
protected void TextBox4_TextChanged(object sender, EventArgs e)
{
}
protected void Button1_Click(object sender, EventArgs e)
{
CreateDropDownLists();
}
protected void Button2_Click(object sender, EventArgs e)
{
string value = "";
string ids = "";
// Cycle through each dropdownlist
for (int i = 0; i < DDLCount; i++)
{
// Find the dropdownlist inside the Panel
DropDownList ddl = Panel3.FindControl("Text" + i) as DropDownList;
// Set the label to the SelectedValue of the dropdownlist
//Label1.Text += ddl.SelectedValue + " : ";
value = ddl.SelectedValue.ToString();
DropDownList dd2 = Panel3.FindControl("Texts" + i) as DropDownList;
// Set the label to the SelectedValue of the dropdownlist
//Label1.Text += ddl.SelectedValue + " : ";
ids = dd2.SelectedValue.ToString();
SqlConnection con = new SqlConnection("Data Source=ABC-0D30299B90A;Initial Catalog=JAPIT;Integrated Security=True");
con.Open();
SqlCommand cmd1 = new SqlCommand("insert into niitstud values('" + value + "','"+ids+"')", con);
cmd1.ExecuteNonQuery();
con.Close();
}
}
}
goltu 0 Newbie Poster
Be a part of the DaniWeb community
We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.