Datalist not rebinding
-
Hi Guys, can anyone help me with with my code. i have a datalist and some textfield with a button to submit. once submitted, the page should rebind it'self, but it seems not working. see my code below: using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; 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; public partial class mssgboard_Messageboard : System.Web.UI.Page { SqlConnection datalistconn; SqlDataReader datalistdr; SqlCommand datalistcmd; SqlConnection conn; //SqlDataReader dr; SqlCommand cmd; void Bindata() { try { datalistconn = new SqlConnection(ConfigurationManager.ConnectionStrings["CmsDb"].ConnectionString); datalistcmd = new SqlCommand("SELECT nam,phone_number,email,comments FROM tbl_mssgboard ORDER BY refid DESC", datalistconn); datalistconn.Open(); datalistdr = datalistcmd.ExecuteReader(); if (datalistdr.Read()) { DataList1.DataSource = datalistdr; DataList1.DataBind(); } } catch (Exception exc) { Response.Write(exc.Message); } finally { datalistdr.Close(); datalistconn.Close(); } } protected void Page_Load(object sender, EventArgs e) { Bindata(); } protected void btnsubmit_Click(object sender, EventArgs e) { try { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CmsDb"].ConnectionString); cmd = new SqlCommand("INSERT INTO tbl_mssgboard(nam,phone_number,email,comments) VALUES(@nam,@phone_number,@email,@comments)", conn); cmd.Parameters.Add("@nam", SqlDbType.VarChar,50); cmd.Parameters.Add("@phone_number", SqlDbType.VarChar, 50); cmd.Parameters.Add("@email", SqlDbType.VarChar, 50); cmd.Parameters.Add("@comments", SqlDbType.VarChar, 50); cmd.Parameters["@nam"].Value = this.txtname.Text; cmd.Parameters["@phone_number"].Value = this.txtphone.Text; cmd.Parameters["@email"].Value = this.txtemail.Text; cmd.Parameters["@comments"].Value = this.txtcomments.Text; conn.Open(); cmd.ExecuteNon
-
Hi Guys, can anyone help me with with my code. i have a datalist and some textfield with a button to submit. once submitted, the page should rebind it'self, but it seems not working. see my code below: using System; using System.Data; using System.Data.SqlClient; using System.Configuration; using System.Collections; 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; public partial class mssgboard_Messageboard : System.Web.UI.Page { SqlConnection datalistconn; SqlDataReader datalistdr; SqlCommand datalistcmd; SqlConnection conn; //SqlDataReader dr; SqlCommand cmd; void Bindata() { try { datalistconn = new SqlConnection(ConfigurationManager.ConnectionStrings["CmsDb"].ConnectionString); datalistcmd = new SqlCommand("SELECT nam,phone_number,email,comments FROM tbl_mssgboard ORDER BY refid DESC", datalistconn); datalistconn.Open(); datalistdr = datalistcmd.ExecuteReader(); if (datalistdr.Read()) { DataList1.DataSource = datalistdr; DataList1.DataBind(); } } catch (Exception exc) { Response.Write(exc.Message); } finally { datalistdr.Close(); datalistconn.Close(); } } protected void Page_Load(object sender, EventArgs e) { Bindata(); } protected void btnsubmit_Click(object sender, EventArgs e) { try { conn = new SqlConnection(ConfigurationManager.ConnectionStrings["CmsDb"].ConnectionString); cmd = new SqlCommand("INSERT INTO tbl_mssgboard(nam,phone_number,email,comments) VALUES(@nam,@phone_number,@email,@comments)", conn); cmd.Parameters.Add("@nam", SqlDbType.VarChar,50); cmd.Parameters.Add("@phone_number", SqlDbType.VarChar, 50); cmd.Parameters.Add("@email", SqlDbType.VarChar, 50); cmd.Parameters.Add("@comments", SqlDbType.VarChar, 50); cmd.Parameters["@nam"].Value = this.txtname.Text; cmd.Parameters["@phone_number"].Value = this.txtphone.Text; cmd.Parameters["@email"].Value = this.txtemail.Text; cmd.Parameters["@comments"].Value = this.txtcomments.Text; conn.Open(); cmd.ExecuteNon
datalistconn is declared as global. When postback, Excepton Occours, becase you can't open SqlConnection if it is already opend