Gridview error in deleting

Posted by Santoshkumar under ASP.NET on 6/15/2011 | Points: 10 | Views : 1050 | Status : [Member] | Replies : 1
I am doing simple application. In object data source using gridview i am getting insert update i am not getting delete

in aspx page
<form id="form1" runat="server">
<div>
<asp:ObjectDataSource ID="osd" runat="server" SelectMethod="GetMovies"
TypeName="ObjectDatasource1.Movie" UpdateMethod="updatemovie"
DeleteMethod="deletemovie">
<UpdateParameters>
<asp:Parameter Name="id" Type="Int32" />
<asp:Parameter Name="title" Type="String" />
<asp:Parameter Name="director" Type="String" />
</UpdateParameters>
<DeleteParameters>
<asp:Parameter Name="id" Type="Int32" />
</DeleteParameters>
</asp:ObjectDataSource>
<asp:GridView ID="GridView1" runat="server" DataSourceID="osd" AutoGenerateEditButton="true"
AutoGenerateDeleteButton="true">
</asp:GridView>
</div>
</form>


in .cs file
public List<Movie> GetMovies()
{
List<Movie> result = new List<Movie>();
SqlConnection con = new SqlConnection(_connectionstring);
SqlCommand cmd = new SqlCommand("SELECT Title,Director,Id From movie", con);
using (con)
{
con.Open();
SqlDataReader reader = cmd.ExecuteReader();
while (reader.Read())
{
Movie mov = new Movie();
mov.Title = (string)reader["Title"];
mov.Id = (int)reader["Id"];
mov.Director = (string)reader["Director"];
result.Add(mov);
}
}
return result;
}

/// <summary>
/// To update Movie
/// </summary>
/// <param name="id"></param>
/// <param name="title"></param>
/// <param name="director"></param>
public void updatemovie(int id, string title, string director)
{
SqlConnection con = new SqlConnection(_connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText = "UPDATE movie SET title=@Title, director=@Director WHERE Id=@Id";
cmd.Parameters.AddWithValue("@Title",title);
cmd.Parameters.AddWithValue("@Director", director);
cmd.Parameters.AddWithValue("@Id", id);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}

public void deletemovie(int id)
{
SqlConnection con = new SqlConnection(_connectionstring);
SqlCommand cmd = new SqlCommand();
cmd.Connection = con;
cmd.CommandText="DELETE movie WHERE Id=@Id";
cmd.Parameters.AddWithValue("@id", id);
using (con)
{
con.Open();
cmd.ExecuteNonQuery();
}
}
plz reply




Responses

Posted by: Lakn2 on: 6/16/2011 [Member] Starter | Points: 25

Up
0
Down
try this
public void deletemovie(int id) 

{
SqlConnection con = new SqlConnection(_connectionstring);
SqlCommand cmd = new SqlCommand("delete movie where id="+id+",con);
con.open();
cmd.executenonquery();
con.close();

}


Thanks&Regards
LakshmiNarayana Nalluri.

Santoshkumar, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response