3
Answers

insert to database with button without updating the page

Joma Alrzini

Joma Alrzini

8y
233
1
I am doing comments on the page and the comment is inserted to database 
 I need to insert with no update whole page  
 
I tried updatePanal  with teriggers but it is not working
this the code

<asp:Button ID="btnIdea" runat="server" Text="Publish Comment" AutoPostback="false"

OnClick="btnIdea_Click" BorderStyle="Double" ForeColor="#000000" />



Code behind


protected void btnIdea_Click(object sender, EventArgs e)

{

if (txtContent.Text == null)

Label7.Text = "Please select User Role";

else

{

SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString);


SqlCommand insert = new SqlCommand("insert into dbo.Comments(Comment, UId, IssueDate, MaID, UserName) values(@Comment, @UId, @IssueDate, @MaID, @UserName)", conn);

int id = Int32.Parse(Request.QueryString["Id"]);

insert.Parameters.AddWithValue("@UId", Session["UniID"].ToString());

insert.Parameters.AddWithValue("@UserName", Session["Full_Name"].ToString());

insert.Parameters.AddWithValue("@MaID", id);

insert.Parameters.AddWithValue("@Comment", txtContent.Text);

insert.Parameters.AddWithValue("@IssueDate", DateTime.Now);

try

{

conn.Open();

insert.ExecuteNonQuery();

txtContent.Text = "";

// Response.Redirect("ACourseManagement.aspx");

}

catch

{

Label7.Visible = true;

Label7.Text = "Error when saving on database";

conn.Close();

}

}


}


Please help  

Answers (3)