SqlDataSource together with MySql ?

Last post 08-24-2007 10:43 AM by nbop. 37 replies.

Sort Posts:

  • Re: SqlDataSource together with MySql ?

    06-29-2007, 6:10 AM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    First, I am thankful for all the info I have seen here.

    Next, I did use the recomplied MySql.dll and works but I cannot get it to work with the SqlDataSource fully in terms of adding records and deleting records. 

    So I looked for another solution...

    Found this older .dll called MarcoMySql.dll out there on http://lab.aspitalia.com/31/MarcoMySql-DataProvider.aspx - after I downloaded his MarcoMySql.dll and copied it to my /bin folder, I was FINALLY able to add this code to my web.config file successfully (on godaddy) to use the <asp:SqlDataSource> tag BUT I am only able to read data.  I'm not able to insert, edit or delete records yet using the <SqlDataSource> tag with it.  I'm thinking it's due to the older .dll I have.

    <system.data>

    <DbProviderFactories>

    <add name="MarcoMySql Data Provider" invariant="MySql.Data.MySqlClient" description="Marco MySql Provider" type="MySql.Data.MySqlClient.MySqlProviderFactory, MarcoMySql.Data, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" />

    </DbProviderFactories>

    </system.data>

    I use this to reference it in the page in the .aspx I am using:

    <% Import Namespace="MySql.Data.MySqlClient" %>

     The challenge I have is that it's an old version as you can see and wanted to know if there was an updated MySql.dll out there with an updated <DbProviderFactories> tag.  My MySql database is 5.0, I have ASP.NET 2.0 runtime.

    Any help out there???  Let me know if you need more info!

  • Re: SqlDataSource together with MySql ?

    06-29-2007, 1:05 PM
    • Loading...
    • Garen
    • Joined on 06-20-2007, 11:54 PM
    • Posts 5

    I got it to work just fine the other day.  There were three steps that had to be taken into account:

    1) Make sure you have the MySQL Visual Studio Plugin installed as well as the .NET connector. If after you place a connection string in your Web.config with "MySql.Data.MySqlClient" as the providerName, and you can't select this connection string from the SqlDataSource wizard--you need to reboot.

    2) Set the DataKeynames property to the name of your key(s) for the applicable table in your GridView/DetailsView control(s).  The wizard doesn't seem to generate this property for you, so it's an extra manual step. The MSDN docs state:

    "You must set the DataKeyNames property for the automatic updating and deleting features of the GridView control to work. The values of these key fields are passed to the data source control in order to match the row to update or delete."

     3) Format your query strings properly when writing your Insert/Update/Delete commands for the SqlDataSource by prefixing the text you want interpolated with a '?' like so:

    DeleteCommand="DELETE FROM <some_table> WHERE EntryNum = ?EntryNum AND SomeVal = ?SomeVal" 

    Here's a snippet of my working example from the .aspx page:

     

    <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:MySQL_Connector_CS %>"
       ProviderName="<%$ ConnectionStrings:MySQL_Connector_CS.ProviderName %>" 
    SelectCommand="SELECT EntryNum, ProjectID, Name, Value, Category FROM project_goals WHERE ProjectID = ?ProjectID;" InsertCommand="INSERT INTO project_goals (ProjectID, Name, Value, Category) VALUES (?ProjectID, ?Name, ?Value, ?Category);" UpdateCommand="UPDATE project_goals SET ProjectID = ?ProjectID, Name = ?Name, Value = ?Value, Category = ?Category WHERE EntryNum = ?EntryNum;" DeleteCommand="DELETE FROM project_goals WHERE EntryNum = ?EntryNum AND ProjectID = ?ProjectID">
    <SelectParameters>
    <asp:ControlParameter ControlID="ddlProject" Name="ProjectID" PropertyName="SelectedValue" Type="Int32" />
    </SelectParameters>
    <InsertParameters>
    <asp:ControlParameter ControlID="ddlProject" Name="ProjectID" PropertyName="SelectedValue" Type="Int32"/>
    <asp:Parameter Name="Name" Type="String" />
    <asp:Parameter Name="Value" Type="Int32" />
    <asp:ControlParameter ControlID="DropDownList1" Name="Category" PropertyName="SelectedValue" Type="String" />
    </InsertParameters>
    ...
    </asp:SqlDataSource>
    Hope that helps. I wasn't able to find a single example online that got me up and running with this and had to scratch my head for awhile.
  • Re: SqlDataSource together with MySql ?

    06-29-2007, 10:13 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    Superb!  I will try this out tonight.  I've been at this for weeks now - and ran out of options. I really appreciate all your effort.  I will try this out and let you know.

  • Re: SqlDataSource together with MySql ?

    06-29-2007, 10:34 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    I am checking with Godaddy since it's shared hosting.  I am hoping they will load it for me.  We will see.  Otherwise, I will have to keep going the same route I am taking now and that's with using SqlAdapters, DataSets, etc. - which is fine - but it would be fun to use the SqlDataSource features.

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 3:03 PM
    • Loading...
    • Zavod
    • Joined on 05-18-2007, 10:45 PM
    • Posts 18
     Great, can you show us the connectionstring also?

     
    And maybe a short example of the gridview with the datakeynames set.

    And what error did you get before you got it working?

    This is what I get even when doing exactly as you (not sure about your connstring yet)

    Unable to find the requested .Net Framework Data Provider.  It may not be installed.

     

     

    Thx. 

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 3:26 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    Called godaddy and they were not aware of the MysSQL Visual Studio Plugin.  I know they support the .NET connector 5.0 and 5.1, but that is al they would tell me.   My other challenge is my web.config file.  I tried adding the following:

    <customErrors mode="Off"/>

    <system.data>

    <DbProviderFactories>

    <add name="MySQL Data Provider" invariant="MySql.Data.MySqlClient"description=".Net Framework Data Provider for MySQL"type="MySql.Data.MySqlClient.MySqlClientFactory, MySql.Data,Version=5.0.1.0,Culture=neutral,PublicKeyToken=c5687fc88969c44d"/>

    </DbProviderFactories>

    </system.data>

    </system.web> </configuration>

    All I get is : 

    Server Error in '/' Application.
    Runtime Error

    When attempting to run "..category.aspx?Category=COLLECTIBLES" which gets picked up by a  <%=Request.QueryString("Category")%> in category.aspx.

    The OTHER way I mentioned does work to a point.  If I use the following declarations...
    Dim MyCommand As MySqlCommand
    Dim myConnection As MySqlConnection
    Dim myDataAdapter As MySqlDataAdapter

    And use the appropriate connection strings and commands  I can connect just fine.
    Just very tricky when it gets to using the insert, update and delete commands.  I get this just attempting to run a page that gets opened for inserting a new record into my mysql database.  Here is the error I get:

    Compilation Error

    Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

    Compiler Error Message: BC30455: Argument not specified for parameter 'options' of 'Public Sub RegisterForEventValidation(options As System.Web.UI.PostBackOptions)'.

    Source Error:

    Line 63: 
    Line 64: If Not Page.IsPostBack Then
    Line 65:     RegisterForEventValidation()
    Line 66:     Dim MyCommand As MySqlCommand
    Line 67:     Dim myConnection As MySqlConnection

     

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 4:04 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    Here is how I can connect to MySql with Godaddy shared hosting.  I can grab data from a table and display it in a gridview.  I am having challenges with the insert, update and delete at the moment - but I got this because I could not find anything out there with the combination I need.  Shared hosting, VB and ASP.NET 2.0 with 5.0 MySql database (still have not found everything I need at the moment - but this is a start)...Hope this helps someone.  Note, if you attempt to click on the data once it is displayed, it is looking for a details.aspx page I got from ASP.NET for Dummies - just fyi!
    Also, remember, I am using the infamous recompiled MySql.dll which can be found in some earlier threads.  Godady shared hosting is set to medium trust so take that into consideration.  I placed the .dll it in my \bin folder and I am really good (if I can just get past these postback issues).

    <%@ Page Explicit="True" Language="VB" Debug="True" %>
    <%
    @ Import Namespace="System.Data" %>
    <%
    @ Import Namespace = "MySql.Data.MySqlClient" %>

    <script language="VB" runat="server">

    Sub Page_Load(sender As Object, e As EventArgs)
         Dim myConnection As MySqlConnection
         Dim myDataAdapter As MySqlDataAdapter
         Dim myDataSet As DataSet
         Dim qryFromDefault As String
         Dim
    strSQL As String
         Dim MySQLConnectionString As String

           MySQLConnectionString = "Server=yourserver;" & _
              "Database=yourdatabasename;" & _
              "Uid=yourlogin;" & _
              "Pwd=yourpassword;" & _
              "Connect Timeout=30;"

    '
    'Comments:
    '
    'Assuming you have a mysql 5.0 database and a table you can query from
    '
    'myConnection =
    New MySqlConnection(MySQLConnectionString)
    '
    'qryFromDefault = Request.QueryString("NameOfParamaterFromCallingPage") - you may use this code if this aspx is called from another page
    'and use it below as a parameter of sorts
    '

    'strSQL = "SELECT * FROM MyTable WHERE (NameOfMyTableColumnName = '" & qryFromDefault & "');"
    '
    'But here is the easier route for now
    '
         strSQL = "SELECT * FROM MyTable"
         myDataAdapter =
    New MySqlDataAdapter(strSQL, myConnection)
         myDataSet =
    New Dataset()
         myDataAdapter.Fill(myDataSet,
    "YourTableNameHere")
         MySQLDataGrid.DataSource = myDataSet.Tables(
    "YourTableNameHere").DefaultView
         MySQLDataGrid.DataBind()
    End Sub

    '
    'More code below I got from somewhere else
    '
    Sub PageIndexChanging(sender as Object, e as GridViewPageEventArgs)
         MySQLDataGrid.PageIndex = e.NewPageIndex
         MySQLDataGrid.DataBind()
    End Sub

    </script>

    <html>

    <body vlink="red">

    <h1>Classy Classifieds</h1>

    <h2>All Ads</h2>

    <form id="Form1" runat="server">
    <asp:GridView ID="MySQLDataGrid" runat="server" AllowPaging="True" AllowSorting="True"
    AutoGenerateColumns="False" BackColor="#FFFF99" BorderColor="Black" BorderStyle="Solid"
    BorderWidth="1px" DataKeyNames="AdNum" Width="100%">
    <HeaderStyle HorizontalAlign="Left" />
     <Columns>

    <
    asp:HyperLinkField DataNavigateUrlFields="AdNum" DataNavigateUrlFormatString="Details.aspx?AdNum={0}"DataTextField="Title" HeaderText="Title" SortExpression="Title" />
    <asp:BoundField DataField="Price" DataFormatString="{0:c}" HeaderText="Price" SortExpression="Price" />
    <asp:BoundField DataField="State" HeaderText="State" SortExpression="State" />

    </Columns>

    </asp:GridView>
    </
    form>

    </body>

    </html>

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 4:15 PM
    • Loading...
    • Zavod
    • Joined on 05-18-2007, 10:45 PM
    • Posts 18

    Mrkaffa you have to add some spaces in your Webconfig-factory, thats why you get runtime error.

    And this thread is about using MySql with SqlDatasource-object, not doing iot with a dataadapter.

     

    Garen:

     
    1. What version of the connector are you using?

    2. Did you add anything into the web.config, if so what?

    3. What does your connectionstring look like?

    4. Gridview-code?
     

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 8:16 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    I added all the code using DataAdapter because I thought that you wanted to see it.  My mis-interpretation. The other this is that I really want to use SqlDataSource ultimately - but all efforts have been exhausted in doing so.  I will see if the space in the factory makes a difference.  That's really my ultimate goal here .

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 10:55 PM
    • Loading...
    • Garen
    • Joined on 06-20-2007, 11:54 PM
    • Posts 5

    Sure--your user id and password will vary of course. I didn't do anything special. I got so many errors awhile ago that I don't think I recall them all off the top of my head. :)  The one you have doesn't look familiar though. From my Web.config:

     

    <connectionStrings>
    <add name="MySQLConnectionString" connectionString="server=localhost;
    user id=pm; password=asdf; database=test; pooling=false;"

    providerName="MySql.Data.MySqlClient"/>
    </connectionStrings>
     
    <asp:GridView ID="GridView1" runat="server" 
    DataSourceID="SqlDataSource1"
    AllowPaging="True"
    AllowSorting="True"
    AutoGenerateDeleteButton="True"
    AutoGenerateEditButton="True"
    DataKeyNames="EntryNum">
    </asp:GridView>
    I'm using the 5.0.7 version of the connector and the 1.0.2 version of the Visual Studio Plugin.  Note that in the beta version 5.1 of the connector and future versions, MySQL AB states that the Visual Studio Plugin will be included in the connector package/download because having them as separate downloads confused people--so says here.


     

  • Re: SqlDataSource together with MySql ?

    06-30-2007, 11:45 PM
    • Loading...
    • mrkaffa
    • Joined on 06-29-2007, 9:32 AM
    • Posts 10

    Geren,

     I'd be interested in your solutions.  I've looked everywhere and have not yet found the complete answer.

    1) Need web.config setup with how you declare the correct version of MySqlClient

    2) Want to know if your code works under full or medium trust.  I am thinking that you have your own server or full access to your server.  I can do eveything just fine on my own local server, but when I am in shared hosting environment as with Godaddy - everything changes.  The pure ASP.NET 2.0 code is simple and straightforward - but the security makes it a challenge for shared hosting.

    Interested in your thoughts or anyone else out there!

  • Re: SqlDataSource together with MySql ?

    07-01-2007, 3:49 AM
    • Loading...
    • Garen
    • Joined on 06-20-2007, 11:54 PM
    • Posts 5

    Everything I did I listed above, and am not sure how to answer your questions 1) and 2). There isn't anything particular that I did to "declare the correct version of MySqlClient."  I simply installed the connector, plugin, and used "MySql.Data.MySqlClient" as the providerName. As to 2), I have no idea--I've never had any issues with trust levels. It ran on my development machine and my own server without me ever having to think about anything trust related.