0

I am trying to link my asp.net Formview to a MySQL database, but with no success.

Take a look at my code and please tell my where is my mistake:

<asp:FormView ID="FormView1" runat="server" DataKeyNames="ImageID" 
        DataSourceID="SqlDataSource1" EnableModelValidation="True">
        <EditItemTemplate>
            ImageID:
            <asp:Label ID="ImageIDLabel1" runat="server" Text='<%# Eval("ImageID") %>' />
            <br />
            Title:
            <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
            <br />
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" 
                Text='<%# Bind("Description") %>' />
            <br />
            <asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" 
                CommandName="Update" Text="Update" />
            &nbsp;<asp:LinkButton ID="UpdateCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </EditItemTemplate>
        <InsertItemTemplate>
            Title:
            <asp:TextBox ID="TitleTextBox" runat="server" Text='<%# Bind("Title") %>' />
            <br />
            Description:
            <asp:TextBox ID="DescriptionTextBox" runat="server" 
                Text='<%# Bind("Description") %>' />
            <br />
            <asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" 
                CommandName="Insert" Text="Insert" />
            &nbsp;<asp:LinkButton ID="InsertCancelButton" runat="server" 
                CausesValidation="False" CommandName="Cancel" Text="Cancel" />
        </InsertItemTemplate>
        <ItemTemplate>
            ImageID:
            <asp:Label ID="ImageIDLabel" runat="server" Text='<%# Eval("ImageID") %>' />
            <br />
            Title:
            <asp:Label ID="TitleLabel" runat="server" Text='<%# Bind("Title") %>' />
            <br />
            Description:
            <asp:Label ID="DescriptionLabel" runat="server" 
                Text='<%# Bind("Description") %>' />
            <br />
            <asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" 
                CommandName="Edit" Text="Edit" />
            &nbsp;<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" 
                CommandName="Delete" Text="Delete" />
            &nbsp;<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" 
                CommandName="New" Text="New" />
        </ItemTemplate>
    </asp:FormView>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:cmsConnectionString %>" 
        InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) VALUES (ImageID, Title, Description)" 
        ProviderName="<%$ ConnectionStrings:cmsConnectionString.ProviderName %>" 
        SelectCommand="SELECT ImageID, Title, Description FROM tbimages" 
        UpdateCommand="UPDATE [tbimages] SET [Title] = ?, [Description] = ? WHERE [ImageID] = ?">

        <InsertParameters>
            <asp:Parameter Name="ImageID" Type="Int64" />
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="Title" Type="String" />
            <asp:Parameter Name="Description" Type="String" />
            <asp:Parameter Name="ImageID" Type="Int64" />
        </UpdateParameters>
    </asp:SqlDataSource>

i already define my connection string in the web config :

    <connectionStrings>
    <add name="cmsConnectionString" connectionString="server=localhost;User Id=root;password=123;database=cms" providerName="MySql.Data.MySqlClient"/>
</connectionStrings>

Please if you have any idea to solve this let me know.

2
  • what is the error message? and where? Commented Jan 22, 2011 at 11:15
  • the error is in the insert . it's giving me every thing but when i'm trying to insert new record it's giving me this error : You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '[tbimages] ([ImageID], [Title], [Description]) VALUES (ImageID, Title, Descripti' at line 1 Commented Jan 22, 2011 at 11:21

2 Answers 2

1

You might want to change the Insert Command like so

InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) VALUES (? , ? , ?)"

UPDATED :

InsertCommand="INSERT INTO [tbimages] ([ImageID], [Title], [Description]) 
 VALUES (?ImageID , ?Title , ?Description)"
Sign up to request clarification or add additional context in comments.

1 Comment

i tried this one before and it gives me another error : Parameter '?' must be defined.!!!
0

check the insert command definition of your datasource. See also: http://msdn.microsoft.com/en-us/library/system.web.ui.webcontrols.sqldatasource.insertcommand.aspx

3 Comments

my problem is with the insert statement> i tried so many ways and the link you gave me it's for MSSQL not MySQL thank you
did you try to append the parameters in the INSERT sytax with an "@"?
Well it does not take parameternames as placeholders but as values. So prepending them with '@' should help. At this stage it should not matter, if MSSQL or MySQL is at the end of the line ...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.