OS: Win XP Pro
Languages: ASP.NET, C#, SQL Server 2000
Program: ASP.NET WebMatrix
Hi again,
I've been working on this for a while now and I don't understand why I can't seem to delete records. Can anyone please help Thanks in advanced
This is my event method when the Delete button is clicked:
public void dbSongDetails_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string connectionString = "my connection string";
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "DELETE FROM [SongDetails] WHERE ([SongDetails].[SongID] = @SongID)";
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(queryString, sqlConnection);
int intSongID = (int)dbSongDetails.DataKeys[e.Item.ItemIndex];
sqlCommand.Parameters.Add("@SongID", System.Data.SqlDbType.Int).Value = intSongID;
int rowsAffected = 0;
sqlConnection.Open();
try { rowsAffected = sqlCommand.ExecuteNonQuery();}
finally
{
sqlConnection.Close();
Response.Write("Media Deleted.");
}
}
{
string connectionString = "my connection string";
System.Data.SqlClient.SqlConnection sqlConnection = new System.Data.SqlClient.SqlConnection(connectionString);
string queryString = "DELETE FROM [SongDetails] WHERE ([SongDetails].[SongID] = @SongID)";
System.Data.SqlClient.SqlCommand sqlCommand = new System.Data.SqlClient.SqlCommand(queryString, sqlConnection);
int intSongID = (int)dbSongDetails.DataKeys[e.Item.ItemIndex];
sqlCommand.Parameters.Add("@SongID", System.Data.SqlDbType.Int).Value = intSongID;
int rowsAffected = 0;
sqlConnection.Open();
try { rowsAffected = sqlCommand.ExecuteNonQuery();}
finally
{
sqlConnection.Close();
Response.Write("Media Deleted.");
}
}
This is my ASP.NET portion that creates the delete buttons:
<asp:datagrid id="dbSongDetails" runat="server" DataKeyField="SongID" OnDeleteCommand="dbSongDetails_DeleteCommand" Height="8px" GridLines="None" CellPadding="4" AutoGenerateColumns="False">
<ItemStyle font-name="Arial" font-size="10pt" forecolor="#000000" />
<HeaderStyle font-name="Arial" font-size="10pt" font-bold="True" backcolor="#003366" forecolor="#FFFFFF" />
<AlternatingItemStyle font-name="Arial" font-size="10pt" backcolor="#CCCCCC" />
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete" runat="server"></asp:ButtonColumn>
<asp:BoundColumn DataField="SongTitle" HeaderText="Song Title" />
<asp:BoundColumn DataField="SongArtist" HeaderText="Song Artist" />
</Columns>
</asp:datagrid>
<ItemStyle font-name="Arial" font-size="10pt" forecolor="#000000" />
<HeaderStyle font-name="Arial" font-size="10pt" font-bold="True" backcolor="#003366" forecolor="#FFFFFF" />
<AlternatingItemStyle font-name="Arial" font-size="10pt" backcolor="#CCCCCC" />
<Columns>
<asp:ButtonColumn Text="Delete" ButtonType="PushButton" CommandName="Delete" runat="server"></asp:ButtonColumn>
<asp:BoundColumn DataField="SongTitle" HeaderText="Song Title" />
<asp:BoundColumn DataField="SongArtist" HeaderText="Song Artist" />
</Columns>
</asp:datagrid>

using the Delete method
J3sus
Khalilme
I tried looking at the source code when I run the page off local server, I noticed this code generated dynamically for each of my rows:
</tr><tr style="color:Black;font-family:Arial;font-size:10pt;">
<td><input type="submit" name="dbSongDetails$ctl02$ctl00" value="delete" /> </td><td>Song1</td><td>Artist 1</td>
</tr><tr style="color:Black;background-color:#CCCCCC;font-family:Arial;font-size:10pt;">
<td><input type="submit" name="dbSongDetails$ctl03$ctl00" value="delete" /> </td><td>Song2</td><td>Artist 2</td>
I noticed that I have some seemingly random numbers generated after dbSongDetails. I'm not sure where it comes from, nor if it has to do with my problem.
*sigh* I wish people on the ASP.NET Forum would answer. I've heard zip from them =(
PC_Doctor_J
Hi Ying,
Since C# is not the problem in your case (the asp.net is) I would post this problem in asp.net forum instead.
takawesttimo
l4ci
Hi!
I'd start by checking a few things by putting a break point in your Delete method, and checking the values of SongID. My guess is that intSongID = (int)dbSongDetails.DataKeys[e.Item.ItemIndex]; isn't matching up to the SQL data table key.
The best place for asking ASP.NET questions is on the ASP.NET community site, and in the forums there. Check out
http://www.asp.net/welcome.aspx tabindex=1&tabid=39.HTH,
PEte