I have this code to delete a photographer from my table, however, photographer_id is a foreign key from my table 'images', and when I delete a photographer I want to delete all the images in the 'images' table by the photographer I am deleting. How do I do that?
...
else if (e.CommandName == "Slet")
{
SqlConnection conn = new SqlConnection();
conn.ConnectionString =
ConfigurationManager.ConnectionStrings["DatabaseConnectionString1"].ToString();
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText = "DELETE FROM photographers WHERE photographer_id = @photographer_id";
cmd.Parameters.Add("@photographer_id", SqlDbType.Int).Value = e.CommandArgument.ToString();
conn.Open();
cmd.ExecuteNonQuery();
conn.Close();
Repeater1.DataBind();
}
this is my IMAGES table :
CREATE TABLE [dbo].[images] (
[image_id] INT IDENTITY (1, 1) NOT NULL,
[image] NVARCHAR (50) NOT NULL,
[FK_photographer] INT NOT NULL,
PRIMARY KEY CLUSTERED ([billede_id] ASC),
CONSTRAINT [FK_fotograf] FOREIGN KEY ([FK_fotograf]) REFERENCES [dbo].[Fotografer] ([fotograf_id]),
);
and this is my PHOTOGRAPHERS table :
CREATE TABLE [dbo].[photographers] (
[photographer_id] INT IDENTITY (1, 1) NOT NULL,
[photographer_name] NVARCHAR (50) NOT NULL,
PRIMARY KEY CLUSTERED ([photographer_id] ASC)
);