You must Sign In to post a response.
  • Category: .NET

    How to get rid of the problem occured when saving two panel control data to database

    hai all
    i have strucked here
    i have atextbox and one button so if i click one in textbox one panelcontrol with table should appear
    so if i enter 2 in textbox two panelcontrols with tables in it should be displayed
    up to here working fine when saving single table valuues to database they are inserting using storedprocesure up to here also fine when inserting two panelcontrols or threepanel with tables data to database here problem coming

    they are saving like
    for ex in database firstname is there they are inserting like ravi,ram,raj in firstname field
    not like ram and raj have to be insert in next records they are saving in one record
    below is my code plz helpme



    public partial class addnewprac : System.Web.UI.Page
    {
    int count;

    protected void Page_Load(object sender, EventArgs e)
    {

    }

    protected void Button1_Click(object sender, EventArgs e)
    {


    count = Convert.ToInt32(TextBox1.Text);
    for (int i = 0; i < count; i++)
    {
    // int index = Panel1.Controls.OfType<TextBox>().ToList().Count + 1;

    Table tbldynamic = new Table();
    tbldynamic.ID = "tbl" + i.ToString();
    TableCell tc = new TableCell();
    TableCell tc1 = new TableCell();
    TableRow tr = new TableRow();
    Label lbl = new Label();

    lbl.ID = "lbluid";
    lbl.Text = "Studentfirstname:";
    tc.Controls.Add(lbl);

    TextBox userid = new TextBox();
    userid.ID = "txtuid"+i;
    tc1.Controls.Add(userid);
    tr.Cells.Add(tc);
    tr.Cells.Add(tc1);
    tbldynamic.Rows.Add(tr);

    tc = new TableCell();
    tc1 = new TableCell();
    tr = new TableRow();
    Label lblName = new Label();
    lblName.ID = "lblName";
    lblName.Text = "Studentlastname:";
    tc.Controls.Add(lblName);
    TextBox txtName = new TextBox();
    txtName.ID = "txtNamea"+i;
    tc1.Controls.Add(txtName);
    tr.Cells.Add(tc);
    tr.Cells.Add(tc1);
    tbldynamic.Rows.Add(tr);
    tc = new TableCell();
    tc1 = new TableCell();
    tr = new TableRow();
    Label goingsc = new Label();
    goingsc.ID = "lblEmaila";
    goingsc.Text = "Going to school:";
    tc.Controls.Add(goingsc);
    CheckBox chk = new CheckBox();
    chk.ID = "chkaa"+i;
    chk.Text = "yes";
    tc1.Controls.Add(chk);

    tr.Cells.Add(tc);
    tr.Cells.Add(tc1);
    tbldynamic.Rows.Add(tr);
    tc = new TableCell();
    tc1 = new TableCell();
    tr = new TableRow();
    Label lblEmailab = new Label();
    lblEmailab.ID = "lblEmail";
    lblEmailab.Text = "SCHOOLNAME:";
    tc.Controls.Add(lblEmailab);
    TextBox schoolname = new TextBox();
    schoolname.ID = "sname"+i;
    tc1.Controls.Add(schoolname);
    tr.Cells.Add(tc);
    tr.Cells.Add(tc1);
    tbldynamic.Rows.Add(tr);
    tc = new TableCell();
    tc1 = new TableCell();
    tr = new TableRow();
    Label grade = new Label();
    grade.ID = "lblEmailabc";
    grade.Text = "grade:";
    tc.Controls.Add(grade);
    DropDownList dpd = new DropDownList();
    ListItem[] items = new ListItem[3];
    items[0] = new ListItem("A Grade", "1");
    items[1] = new ListItem("B Grade", "2");

    items[2] = new ListItem("C Grade", "3");
    dpd.Items.AddRange(items);
    dpd.DataBind();
    dpd.ID = "select"+i;

    tc1.Controls.Add(dpd);
    tr.Cells.Add(tc);
    tr.Cells.Add(tc1);
    tbldynamic.Rows.Add(tr);


    Panel1.Controls.Add(tbldynamic);
    }
    }
    protected void btnSubmit_click(object sender, EventArgs e)
    {
    TextBox firstname = (TextBox)Panel1.FindControl("txtuid");

    TextBox lastname = (TextBox)Panel1.FindControl("txtNamea");
    CheckBox chkk = (CheckBox)Panel1.FindControl("chkaa");
    TextBox txttt = (TextBox)Panel1.FindControl("sname");
    DropDownList dda = (DropDownList)Panel1.FindControl("select");
    Response.Write(firstname.Text);
    Response.Write(lastname.Text);
    Response.Write(chkk.Text);
    Response.Write(txttt.Text);
    Response.Write(dda.Text);
    SqlConnection con = new SqlConnection("Data Source=HOME;Initial Catalog=login;Integrated Security=True");

    SqlCommand cmd = new SqlCommand("[student info]", con);
    cmd.CommandType = System.Data.CommandType.StoredProcedure;
    con.Open();
    cmd.Parameters.AddWithValue("@sname", firstname.Text);
    cmd.Parameters.AddWithValue("@slname", lastname.Text);
    cmd.Parameters.AddWithValue("@gsch", chkk.Text);
    cmd.Parameters.AddWithValue("@ssname", txttt.Text);
    cmd.Parameters.AddWithValue("@sgr", dda.SelectedItem.Text);
    cmd.ExecuteNonQuery();

    con.Close();

    }
    my stored procesure is
    USE [login]
    GO

    /****** Object: StoredProcedure [dbo].[student info] Script Date: 11/03/2015 15:06:26 ******/
    SET ANSI_NULLS ON
    GO

    SET QUOTED_IDENTIFIER ON
    GO



    CREATE procedure [dbo].[student info]
    (
    @sname nvarchar(50),
    @slname nvarchar(50),
    @gsch bit,
    @ssname nvarchar(50),
    @sgr nvarchar(50)
    )
    as
    begin

    insert into studentdetails values(@sname,@slname,@gsch,@ssname,@sgr)
    end



    GO


    any body helpme
    thank and regards
    raviram
  • #763169
    The logic you are using to generate dynamic textbox for multiple rows. please use the same logic to iterate through multiple rows and calling database procedure for each row insert record in the database.
    Miss. Jain
    Microsoft Certified Technology Specialist in .Net


Sign In to post your comments