Jump to content

Creating bolg in asp


gotu
 Share

Recommended Posts

i m new to W3Schoolsi have made a asp.net single page in which i m using a textbox and a button.also i have make use of dynamic labels on the same page, which will store the text entered in textbox. this is connected to SQL server database where text message is stored.The problem is that after i submit the entered text wih the help of buttonthe output of privous screen remains as it is and all message will reprint on the screen and cause redundancyso kindly help me in sorting this problem

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{        static Label[] lab_arr = new Label[100];    static int lab_count;    protected void Page_Load(object sender, EventArgs e)    {                        SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");        con.Open();        DataSet ds = new DataSet();        SqlDataAdapter da = new SqlDataAdapter("select * from trial", con);        da.Fill(ds, "trial");                for (int i = 0; i < ds.Tables["trial"].Rows.Count; i++)        {            try            {                Label new_label = new Label();                lab_arr[lab_count++] = new_label;                add_label(new_label);                                     new_label.Text = ds.Tables[0].Rows[i][0].ToString();                string q = "select * from trial";                SqlCommand cmd = new SqlCommand(q, con);                SqlDataReader dr = cmd.ExecuteReader();                new_label.Text = dr.GetString(i);                            }            catch (Exception ex)            {            }        }        try        {            if (lab_arr[0] is Label)            {                foreach (Label label in lab_arr)                {                    add_label(label);                }            }        }        catch (Exception ex)        {                    }    }    protected void add_label(Label label)    {        try        {            Controls.Add(label);            SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");            con.Open();            string q = "select * from trial";            SqlCommand cmd = new SqlCommand(q, con);            SqlDataReader dr = cmd.ExecuteReader();            while (dr.Read())            {                               label.Text = dr.GetString(1);            }            con.Close();        }        catch (Exception ex)        {                    }    }                protected void Button1_Click(object sender, EventArgs e)    {        SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");        con.Open();        try        {                        Label new_label = new Label();            lab_arr[lab_count++] = new_label;            add_label(new_label);            new_label.Text = TextBox1.Text;            string q = "insert into trial values('"+TextBox1.Text+"')";            SqlCommand cmd = new SqlCommand(q, con);            cmd.ExecuteNonQuery();            TextBox1.Text = "";            con.Close();                                   }        catch (Exception ex)        {                    }    }    }

i m providing my code below hope you will help me

Link to comment
Share on other sites

sorry for the format of code this is the correct format of code please help me

using System;using System.Data;using System.Configuration;using System.Web;using System.Web.Security;using System.Web.UI;using System.Web.UI.WebControls;using System.Web.UI.WebControls.WebParts;using System.Web.UI.HtmlControls;using System.Data.SqlClient;public partial class _Default : System.Web.UI.Page{        static Label[] lab_arr = new Label[100];    static int lab_count;    protected void Page_Load(object sender, EventArgs e)    {                        SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");        con.Open();        DataSet ds = new DataSet();        SqlDataAdapter da = new SqlDataAdapter("select * from trial", con);        da.Fill(ds, "trial");                for (int i = 0; i < ds.Tables["trial"].Rows.Count; i++)        {            try            {                Label new_label = new Label();                lab_arr[lab_count++] = new_label;                add_label(new_label);                                     new_label.Text = ds.Tables[0].Rows[i][0].ToString();                string q = "select * from trial";                SqlCommand cmd = new SqlCommand(q, con);                SqlDataReader dr = cmd.ExecuteReader();                new_label.Text = dr.GetString(i);                            }            catch (Exception ex)            {            }        }        try        {            if (lab_arr[0] is Label)            {                foreach (Label label in lab_arr)                {                    add_label(label);                }            }        }        catch (Exception ex)        {                    }    }    protected void add_label(Label label)    {        try        {            Controls.Add(label);            SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");            con.Open();            string q = "select * from trial";            SqlCommand cmd = new SqlCommand(q, con);            SqlDataReader dr = cmd.ExecuteReader();            while (dr.Read())            {                               label.Text = dr.GetString(1);            }            con.Close();        }        catch (Exception ex)        {                    }    }                protected void Button1_Click(object sender, EventArgs e)    {        SqlConnection con = new SqlConnection("server=(local);uid=sa;pwd=sa;database=asim");        con.Open();        try        {                        Label new_label = new Label();            lab_arr[lab_count++] = new_label;            add_label(new_label);            new_label.Text = TextBox1.Text;            string q = "insert into trial values('"+TextBox1.Text+"')";            SqlCommand cmd = new SqlCommand(q, con);            cmd.ExecuteNonQuery();            TextBox1.Text = "";            con.Close();                                   }        catch (Exception ex)        {                    }    }    }

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...