简体   繁体   中英

How to cancel linkButton_Click() Event

I have a linkbutton on my ASP.Net site that allows enduser to add a song to the db. I want to check server side if the song already exists before its getting added. This is my code:

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();
    }

now i want to cancel the event if it comes into the

if(cnt ==1) 
{

}

block but my EventArgs e doesnt have a cancel method. Can i just switch to another type of Argument which got a cancel method and if so which one?

thank you!

You could try below:

if(cnt ==1) 
{
   return;
}

Hope this will help !!

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM