Updating Database on Button Click

Discussion in 'ASP.NET / ASP.NET Core' started by collegefootballsim.com, Dec 28, 2012.

  1. I'm trying to update my database with a button_click event.

    I'm using VWD 2010, SQL 2008 and am coding in VB.net

    Here is what I have in the code behind

    Imports System.Data.SqlClient
    Imports System.Data

    Partial Class MyProgram2
    Inherits System.Web.UI.Page

    Protected Sub Button1_Click(sender As Object, e As System.EventArgs)

    Dim con As New SqlConnection("MyConnectionString")
    con.Open()
    Dim command As New SqlCommand("UPDATE User_Profile SET ProgramNumber = @ProgramNumber WHERE UserName = @UserName", con)
    command.Parameters.Add("@ProgramNumber", SqlDbType.Int).Value = DropDownList1.SelectedValue
    command.Parameters.Add("@UserName", SqlDbType.NVarChar).Value = LoginName1
    command.ExecuteNonQuery()
    con.Close()

    End Sub

    End Class

    I'm not getting any errors, it's just not working. Can anyone help me figure out what I'm doing wrong?

    Thanks in advance!
     
  2. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    Are you running the application on the web server or your desktop? Are you using the correct connection string?
     
  3. Hello Tasslehoff,

    I'm running it on the web. I've tried using my "LocalSqlServer" Connection String that I have set up in my web.config and putting the connection string in the parenthesis directly.

    No luck either way.

    My connection strings work on all my asp Gridviews and what not on my other pages.
     
  4. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    I think this is the line that's wrong:

    You need to either hard code the connection string or pull it from the web.config file. Please see this link for some syntax:

    http://weblogs.asp.net/owscott/arch...-strings-from-web.config-in-ASP.NET-v2.0.aspx

    If you're trying to pull it from the web.config file, it probably should look something like this:

     
  5. Ok, changed the first line in my button click event to:

    Dim con As New SqlConnection((ConfigurationManager.ConnectionStrings("LocalSqlServer").ConnectionString))


    In my web.config I have:

    Code:
    <connectionStrings>
        <clear />
        <add name="LocalSqlServer" connectionString="Data Source=tcp:sql2k802.discountasp.net;Initial Catalog=SQL2008_627938_prsports;User ID=SQL2008_627938_prsports_user;Password=*******;" />
        <add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ASPNETDB.MDF;Integrated Security=True;User Instance=True"
          providerName="System.Data.SqlClient" />
        
      </connectionStrings>
    Still stuck :confused:
     
  6. If it is a connection string error you should at least get some kind of error. What I suggest is that you alter the connection string and set it to something you know it doesn't exists. If it throws an error then you know that your button_click event is firing.

    Then I would check the actual update SQL statement. Check to make sure that the values are being passed to SQL statement. You may want to for the time being hard code the Update SQL statement rather then passing values from the forms page to your event.
     
  7. Guys, thanks for your help.

    Finally got it working. Turns out my issue was the DropDownList1.SelectedValue.

    Changed it to DropDownList1.SelectedValue.ToString()

    That got it working
     
  8. RayH

    RayH DiscountASP.NET Lackey DiscountASP.NET Staff

    I was going to post some code I got working, but glad you figured it out. :)
     

Share This Page