void updateAuthor() { try { SqlConnection conn = new SqlConnection(strConn); if (conn.State == ConnectionState.Closed) { conn.Open(); } //UPDATE 語法 SqlCommand sqlCmd = new SqlCommand( "UPDATE author_master_table SET " + "author_name = @author_name WHERE author_id = '" + AuthorIDTextBox.Text.Trim() + "'", conn); //接收TextBox值 sqlCmd.Parameters.AddWithValue("@author_name", AuthorNameTextBox.Text.Trim()); //Trim 移除前後空白 sqlCmd.ExecuteNonQuery(); conn.Close(); Response.Write("<script>alert('Author update successful!');</script>"); clearForm(); AuthorGridView.DataBind(); } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
void signUpNewAuthor() { try { SqlConnection conn = new SqlConnection(strConn); if (conn.State == ConnectionState.Closed) { conn.Open(); } //INSERT INTO語法 SqlCommand sqlCmd = new SqlCommand( "INSERT INTO author_master_table" + "(author_id, author_name)" + "VALUES(@author_id, @author_name)", conn); //接收TextBox值 sqlCmd.Parameters.AddWithValue("@author_id", AuthorIDTextBox.Text.Trim()); //Trim 移除前後空白 sqlCmd.Parameters.AddWithValue("@author_name", AuthorNameTextBox.Text.Trim()); sqlCmd.ExecuteNonQuery(); conn.Close(); Response.Write("<script>alert('Add author successful!');</script>"); clearForm(); AuthorGridView.DataBind(); //Grid auto refresh after insert data } catch (Exception ex) { Response.Write("<script>alert('" + ex.Message + "');</script>"); } }
protected void DeleteButton_Click(object sender, EventArgs e) { if (authorIdTextBox.Text=="") { Response.Write("<script>alert('Please Insert Author Id');</script>"); } else { author = new Author(); author.AuthorId = Convert.ToInt32(authorIdTextBox.Text); string message = authorManager.DeleteAuthor(author.AuthorId); Response.Write("<script>alert('" + message + "');</script>"); AuthorGridView.DataBind(); } }
protected void AddButton_Click(object sender, EventArgs e) { if (authorNameTextBox.Text=="") { Response.Write("<script>alert('Please Insert Author Name');</script>"); } else { author = new Author(); author.AuthorName = authorNameTextBox.Text; string message = authorManager.SaveAuthor(author); Response.Write("<script>alert('" + message + "');</script>"); AuthorGridView.DataBind(); } }
protected void Page_Load(object sender, EventArgs e) { AuthorGridView.DataBind(); }