locked
Null Exception on Insert Into MySql RRS feed

  • Question

  • User-1402564948 posted

    Hello,

    I recently moved to a new server and I'm trying to use a MySql Database rather than MSSQL as coded for.

    My server is sharing several domains and those domains are sharing the database.

    Whenever I click the button, It always gives me a null reference.  There are records being added to the table, but not the values.  Only the Int.

    I've checked to make sure I have mysql.data.sqlclient and related assemblies.

    I guess one of my questions would be ... should I be using localhost or the main server ip address?  Should I be using the server name rather than the ip address?  Does it matter?

    Protected Sub PostWall2_Click(sender As Object, e As EventArgs)
            Dim strConnString As [String] = System.Configuration.ConfigurationManager.ConnectionStrings("Server=********;Database=******;UID=******;Pwd=*********").ConnectionString
            Dim con As New MySqlConnection(strConnString)
            Dim strQuery As String = "INSERT [INTO] Wall [(Body, wURL, Id)];"
            Dim cmd As New MySqlCommand(strQuery)
            'You need to use the GetUserId() method to get the userid value
    
            Using myConnection As New MySqlConnection("Server=******;Database=********;UID=*******;Pwd=********")
    
                myConnection.Open()
    
                Dim myCommand As New MySqlCommand(strQuery, myConnection)
                'myCommand.Parameters.AddWithValue("@Subject", Subject.Text.Trim())
                cmd.Parameters.Add(New MySqlParameter("@Body", Body.Text.Trim))
                cmd.Parameters.Add(New MySqlParameter("@wURL", wURL.Text.Trim))
                cmd.Parameters.Add(New MySqlParameter("@Id", User.Identity.GetUserId().ToString))
    
                myCommand.ExecuteNonQuery()
                myConnection.Close()
                GridView1.DataBind()
            End Using
    
            'Subject.Text = String.Empty
    
            Body.Text = String.Empty
            wURL.Text = String.Empty
            SettingsUpdatedMessage.Visible = True
        End Sub
    <div id="HPDiv" runat="server" class="PostDivTop" visible="false" style="background-color: lightgrey;">
    
    <asp:TextBox ID="Body" runat="server" CssClass="HBodyPost" Placeholder="Share an update" TextMode="MultiLine" ValidationGroup="PostMsg" Visible="False"></asp:TextBox>
    
    
    <asp:TextBox ID="wURL" runat="server" CssClass="HBodyPostURL" Placeholder="http://www.helpsavecharlie.org" ValidationGroup="PostMsg" Visible="False"></asp:TextBox>
    
    </div>
                                
    <div class="HButtonDiv">
    
    <asp:Button ID="PostWall2" runat="server" OnClick="PostWall2_Click" BackColor="Tan" CssClass="HPostButton" ForeColor="Brown" Text="MFAN" ValidationGroup="PostMsg" />
    
    </div>
    [NullReferenceException: Object reference not set to an instance of an object.]
       mobilefan.Home.PostWall2_Click(Object sender, EventArgs e) +76
       System.Web.UI.WebControls.Button.OnClick(EventArgs e) +9628442
       System.Web.UI.WebControls.Button.RaisePostBackEvent(String eventArgument) +103
       System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePostBackEvent(String eventArgument) +10
       System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler sourceControl, String eventArgument) +13
       System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData) +35
       System.Web.UI.Page.ProcessRequestMain(Boolean includeStagesBeforeAsyncPoint, Boolean includeStagesAfterAsyncPoint) +1724
    

    Thank you!

    Monday, February 8, 2016 1:21 AM

All replies

  • User2103319870 posted

    On which line you are getting exception

    Monday, February 8, 2016 1:44 AM
  • User-1402564948 posted

    how do I know?  you want me to debug?  when I debug I don't get any errors.

    Monday, February 8, 2016 2:48 AM
  • User-1402564948 posted

    So, I've switched to an ODBC Connection.  I'm now connecting remotely, but I still get the null exception error.

    Obviously, something is null somewhere.  Any thoughts?  Am I not getting the userid correctly?  Am I not inserting something?

    Thanks.

    Monday, February 8, 2016 3:26 PM