Frage

Recently I have been watching Plural Sight ASP.NET videos on data binding, and I came across declarative data source controls such as <asp:SqlDataSource />.

I am having trouble understanding why I am unable to insert a default movie title and release date into my movies table in a sql server 2008 database. I haven't had any trouble up until this point, and have been able to access the table via SelectCommand through a gridView, but when I click on the insert movie button, the new manually made movie parameters aren't added to the table.

Here is my code for a better look:

InsertCommand="INSERT INTO movies (title, release_date) VALUES (@title, @release_date)"
SelectCommand="SELECT TOP 30 movie_id, title, release_date FROM movies ORDER BY movie_id DESC"
runat="server" ID="_moviesDataSource" DataSourceMode="DataReader">
<InsertParameters>
    <asp:Parameter Name="title" />
    <asp:Parameter Name="release_date"/>
</InsertParameters>
</asp:SqlDataSource>
<asp:Button ID="Button1" runat="server" Text="Insert new movie" 
    onclick="Button1_Click" /><br /><br />
<asp:GridView ID="_moviesGridInsert" runat="server" DataSourceID="_moviesDataSource">
</asp:GridView>

As you may notice above, I did not have the Type="..." in the Parameter asp tag, but before this, i had the types equal to string for title and dateTime for release_date. Could the problem possibly be that the values I have set in my database be different then the ones I declared above?

C# back-end of it:

protected void Button1_Click(object sender, EventArgs e)
{
    Page.MaintainScrollPositionOnPostBack = true;

    // TODO - Insert movie (manually inserted with just user input of button click)
    _moviesDataSource.InsertParameters["title"].DefaultValue = "My movie";
    _moviesDataSource.InsertParameters["release_date"].DefaultValue = "01/01/2008";
    _moviesDataSource.Insert();
}

I'm not allowed to post images yet, so adding the grid is not possible, even though it would help if you could view the table itself... But anyways, the table, once the button is clicked, stays the same, and when I look in the database, the new values aren't added.

Keine korrekte Lösung

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top