2014-03-30 49 views
0

我在我的ASP.Net站點上有一個鏈接按鈕,允許最終用戶將歌曲添加到數據庫。 我想檢查服務器端,如果歌曲已經存在,它已被添加。這是我的代碼:如何取消linkBut​​ton_Click()事件

protected void linkButtonInsert_Click(object sender, EventArgs e) 
    { 
     String newArtistName = ((TextBox)myGridView.FooterRow.FindControl("textBoxInsertArtist")).Text; 
     String newTitle = ((TextBox)myGridView.FooterRow.FindControl("textBoxInsertTitle")).Text; 
     int newGenreId = ((DropDownList)myGridView.FooterRow.FindControl("ddlInsertGenre")).SelectedIndex; 
     int newArtistId = -1; 
     bool isNewEntry = false; 

     //check if new artist exists and get artistId   
     using (SqlConnection con = new SqlConnection(CS)) 
     { 
      //Sql to check if Artist already exists. If true, return id, if false insert new artist into tblArtist and return id. 
      SqlCommand cmd = new SqlCommand("if NOT EXISTS (select * from tblArtist where Artist= @newArtistName) INSERT INTO tblArtist (Artist) Output inserted.ID, 'TRUE' as isNewEntry Values(@newArtistName) ELSE Select ID from tblArtist Where Artist = @newArtistName;", con); 
      cmd.Parameters.AddWithValue(@"newArtistName", newArtistName); 
      con.Open(); 
      //newArtistId = (int)cmd.ExecuteScalar(); 
      SqlDataReader rd = cmd.ExecuteReader(); 
      while (rd.Read()) 
      { 
       newArtistId = Convert.ToInt16(rd["ID"]); 
       isNewEntry = Convert.ToBoolean(rd["isNewEntry"]); 
      } 

     } 

     //if isNewEntry == false: check if song already exists: if yes: cancel 
     if (!isNewEntry) 
     { 
      int cnt = -1; 
      using (SqlConnection con = new SqlConnection(CS)) 
      { 
       SqlCommand cmd = new SqlCommand("Select Count(*) from tblSong WHERE Title = @newTitle AND ArtistId = @newArtistId;", con); 
       cmd.Parameters.AddWithValue(@"newTitle", newTitle); 
       cmd.Parameters.AddWithValue(@"newArtistId", newArtistId); 
       con.Open(); 
       cnt = (int)cmd.ExecuteScalar(); 
      } 
      if(cnt == 1) 
      { 

      } 

     } 

     //insert new song 
     using (SqlConnection con = new SqlConnection(CS)) 
     { 
      SqlCommand cmd = new SqlCommand("Insert into tblSong (Title, ArtistId, GenreId) Values (@newTitle, @newArtistId, @newGenreId);", con); 
      cmd.Parameters.AddWithValue(@"newTitle", newTitle); 
      cmd.Parameters.AddWithValue(@"newArtistId", newArtistId); 
      cmd.Parameters.AddWithValue(@"newGenreId", newGenreId); 
      con.Open(); 
      cmd.ExecuteNonQuery(); 
     } 

     //update grid 
     myGridView.DataBind(); 
    } 

現在我想,如果它進入了

if(cnt ==1) 
{ 

} 

塊,但我EventArgs的可是沒有取消方法來取消事件。我可以切換到另一種類型的獲得取消方法的參數,如果有的話,哪一個?

謝謝!

+0

爲什麼你不能'返回'? – dursk

回答

1

你可以嘗試以下:

if(cnt ==1) 
{ 
    return; 
} 

希望這將幫助!