Updating a record - asp.net with vb.net

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • adz1809
    New Member
    • Dec 2006
    • 27

    Updating a record - asp.net with vb.net

    I'm trying to update a record using "System.Data.Sq lClient" in asp.net.

    Here is my code:

    [HTML]<%@ import namespace="Syst em.Data" %>
    <%@ import namespace="Syst em.Data.SqlClie nt" %>
    <Script Language="VB" Runat="Server">

    dim objCmd As SqlCommand
    dim objRdr As SqlDataReader
    dim strCmd As String


    Sub Page_Load()

    CheckActive()
    LogActivity()

    sysMenu = true

    BindData()

    end sub

    Sub BindData()

    dim dsn As String = ConfigurationSe ttings.AppSetti ngs("SystemConn ectionNew")

    dim objConn as New SqlConnection(d sn)

    objCmd = New SqlCommand("SEL ECT * FROM tbl_ACCO_EU_Con tacts WHERE EndUserID=" & Request.QuerySt ring("id"), objConn )

    objConn.open()

    objRdr = objCmd.ExecuteR eader()
    'dbcontacts.Dat aSource = objRdr

    'dbcontacts.Dat aBind()

    While objRdr.Read()

    If not isDbNull(objRdr .Item("Title")) Then
    textTitle.Text = objRdr.Item("Ti tle")
    End If

    If not isDbNull(objRdr .Item("FirstNam e")) Then
    textFirstName.T ext = objRdr.Item("Fi rstName")
    End If

    If not isDbNull(objRdr .Item("LastName ")) Then
    textLastName.Te xt = objRdr.Item("La stName")
    End If

    If not isDbNull(objRdr .Item("CompanyN ame")) Then
    textCompanyName .Text = objRdr.Item("Co mpanyName")
    End If

    If not isDbNull(objRdr .Item("CompanyP osition")) Then
    textCompanyPosi tion.Text = objRdr.Item("Co mpanyPosition")
    End If

    If not isDbNull(objRdr .Item("CompanyA ddress1")) Then
    textCompanyAddr ess1.Text = objRdr.Item("Co mpanyAddress1")
    End If

    If not isDbNull(objRdr .Item("CompanyA ddress2")) Then
    textCompanyAddr ess2.Text = objRdr.Item("Co mpanyAddress2")
    End If

    If not isDbNull(objRdr .Item("CompanyA ddress3")) Then
    textCompanyAddr ess3.Text = objRdr.Item("Co mpanyAddress3")
    End If

    If not isDbNull(objRdr .Item("CompanyA ddress4")) Then
    textCompanyAddr ess4.Text = objRdr.Item("Co mpanyAddress4")
    End If

    If not isDbNull(objRdr .Item("CompanyT own")) Then
    textCompanyTown .Text = objRdr.Item("Co mpanyTown")
    End If

    If not isDbNull(objRdr .Item("CompanyC ounty")) Then
    textCompanyCoun ty.Text = objRdr.Item("Co mpanyCounty")
    End If

    If not isDbNull(objRdr .Item("CompanyP ostcode")) Then
    textCompanyPost code.Text = objRdr.Item("Co mpanyPostcode")
    End If

    If not isDbNull(objRdr .Item("CompanyC ountry")) Then
    textCompanyCoun try.Text = objRdr.Item("Co mpanyCountry")
    End If

    If not isDbNull(objRdr .Item("CompanyE MailAddress")) Then
    textCompanyEMai lAddress.Text = objRdr.Item("Co mpanyEMailAddre ss")
    End If

    If not isDbNull(objRdr .Item("CompanyT elephone")) Then
    textCompanyTele phone.Text = objRdr.Item("Co mpanyTelephone" )
    End If

    If not isDbNull(objRdr .Item("CompanyF ax")) Then
    textTitle.Text = objRdr.Item("Co mpanyFax")
    End If

    If not isDbNull(objRdr .Item("CompanyD irectDial")) Then
    textCompanyDire ctDial.Text = objRdr.Item("Co mpanyDirectDial ")
    End If

    If not isDbNull(objRdr .Item("CompanyW ebAddress")) Then
    textCompanyWebA ddress.Text = objRdr.Item("Co mpanyWebAddress ")
    End If

    If not isDbNull(objRdr .Item("CompanyS IC")) Then
    textCompanySIC. Text = objRdr.Item("Co mpanySIC")
    End If

    If not isDbNull(objRdr .Item("CompanyS ize")) Then
    textCompanySize .Text = objRdr.Item("Co mpanySize")
    End If

    If not isDbNull(objRdr .Item("Personal Address1")) Then
    textPersonalAdd ress1.Text = objRdr.Item("Pe rsonalAddress1" )
    End If

    If not isDbNull(objRdr .Item("Personal Address2")) Then
    textPersonalAdd ress2.Text = objRdr.Item("Pe rsonalAddress2" )
    End If

    If not isDbNull(objRdr .Item("Personal Address3")) Then
    textPersonalAdd ress3.Text = objRdr.Item("Pe rsonalAddress3" )
    End If

    If not isDbNull(objRdr .Item("Personal Address4")) Then
    textPersonalAdd ress4.Text = objRdr.Item("Pe rsonalAddress4" )
    End If

    If not isDbNull(objRdr .Item("Personal Town")) Then
    textPersonalTow n.Text = objRdr.Item("Pe rsonalTown")
    End If

    If not isDbNull(objRdr .Item("Personal County")) Then
    textPersonalCou nty.Text = objRdr.Item("Pe rsonalCounty")
    End If

    If not isDbNull(objRdr .Item("Personal PostCode")) Then
    textPersonalPos tCode.Text = objRdr.Item("Pe rsonalPostCode" )
    End If

    If not isDbNull(objRdr .Item("Personal Country")) Then
    textPersonalCou ntry.Text = objRdr.Item("Pe rsonalCountry")
    End If

    If not isDbNull(objRdr .Item("EndUserI D")) Then
    textEndUserID.T ext = objRdr.Item("En dUserID")
    End If

    End While

    objRdr.Close()

    objConn.Close()

    btnUpdate.Enabl ed = True

    End Sub

    sub UpdateRec(s As Object, e As EventArgs)

    dim dsn As String = ConfigurationSe ttings.AppSetti ngs("SystemConn ectionNew")

    dim objConn as New SqlConnection(d sn)

    strCmd = "UPDATE tbl_ACCO_EU_Con tacts SET Title=@Title, FirstName=@Firs tName, LastName=@LastN ame, CompanyPosition =@CompanyPositi on, CompanyName=@Co mpanyName, CompanyAddress1 =@CompanyAddres s1, CompanyAddress2 =@CompanyAddres s2, CompanyAddress3 =@CompanyAddres s3, CompanyAddress4 =@CompanyAddres s4, CompanyTown=@Co mpanyTown, CompanyCounty=@ CompanyCounty, CompanyPostcode =@CompanyPostco de, CompanyEMailAdd ress=@CompanyEM ailAddress, CompanyTelephon e=@CompanyTelep hone, CompanyFax=@Com panyFax, CompanyDirectDi al=@CompanyDire ctDial, CompanyWebAddre ss=@CompanyWebA ddress, CompanySIC=@Com panySIC, CompanySize=@Co mpanySize, PersonalAddress 1=@PersonalAddr ess1, PersonalAddress 3=@PersonalAddr ess3, PersonalAddress 4=@PersonalAddr ess4, PersonalTown=@P ersonalTown, PersonalCounty= @PersonalCounty , PersonalPostCod e=@PersonalPost Code, PersonalCountry =@PersonalCount ry WHERE EndUserID=@EndU serID"

    Dim objCmd As New SqlCommand(strC md, objConn)

    objCmd.Paramete rs.Add("@Title" , textTitle.Text)
    objCmd.Paramete rs.Add("@FirstN ame", textFirstName.T ext)
    objCmd.Paramete rs.Add("@LastNa me", textLastName.Te xt)
    objCmd.Paramete rs.Add("@Compan yPosition", textCompanyPosi tion.Text)
    objCmd.Paramete rs.Add("@Compan yName", textCompanyName .Text)
    objCmd.Paramete rs.Add("@Compan yAddress1", textCompanyAddr ess1.Text)
    objCmd.Paramete rs.Add("@Compan yAddress2", textCompanyAddr ess2.Text)
    objCmd.Paramete rs.Add("@Compan yAddress3", textCompanyAddr ess3.Text)
    objCmd.Paramete rs.Add("@Compan yAddress4", textCompanyAddr ess4.Text)
    objCmd.Paramete rs.Add("@Compan yTown", textCompanyTown .Text)
    objCmd.Paramete rs.Add("@Compan yCounty", textCompanyCoun ty.Text)
    objCmd.Paramete rs.Add("@Compan yPostcode", textCompanyPost code.Text)
    objCmd.Paramete rs.Add("@Compan yEMailAddress", textCompanyEMai lAddress.Text)
    objCmd.Paramete rs.Add("@Compan yTelephone", textCompanyTele phone.Text)
    objCmd.Paramete rs.Add("@Compan yFax", textCompanyFax. Text)
    objCmd.Paramete rs.Add("@Compan yDirectDial", textCompanyDire ctDial.Text)
    objCmd.Paramete rs.Add("@Compan yWebAddress", textCompanyWebA ddress.Text)
    objCmd.Paramete rs.Add("@Compan ySIC", textCompanySIC. Text)
    objCmd.Paramete rs.Add("@Compan ySize", textCompanySize .Text)
    objCmd.Paramete rs.Add("@Person alAddress1", textPersonalAdd ress1.Text)
    objCmd.Paramete rs.Add("@Person alAddress2", textPersonalAdd ress2.Text)
    objCmd.Paramete rs.Add("@Person alAddress3", textPersonalAdd ress3.Text)
    objCmd.Paramete rs.Add("@Person alAddress4", textPersonalAdd ress4.Text)
    objCmd.Paramete rs.Add("@Person alTown", textPersonalTow n.Text)
    objCmd.Paramete rs.Add("@Person alCounty", textPersonalCou nty.Text)
    objCmd.Paramete rs.Add("@Person alPostCode", textPersonalPos tCode.Text)
    objCmd.Paramete rs.Add("@Person alCountry", textPersonalCou ntry.Text)
    objCmd.Paramete rs.Add("@EndUse rID", textEndUserID.T ext)

    objConn.open()

    objCmd.ExecuteN onQuery()

    objConn.close()

    response.Redire ct("record_srch .aspx")

    end sub[/HTML]

    I'm not getting any error message back when the form is submitted, but the record is not updating.

    Any ideas?

    Cheers

    Adz
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #2
    Is the response.redire ct working?

    btw - did you choose that color?

    Comment

    • adz1809
      New Member
      • Dec 2006
      • 27

      #3
      Originally posted by kenobewan
      Is the response.redire ct working?

      btw - did you choose that color?

      Yep, the redirect works fine and no I didn't choose the colour, it did that all by itself.

      Does the redirect have an effect on the update?

      Comment

      • kenobewan
        Recognized Expert Specialist
        • Dec 2006
        • 4871

        #4
        No but it shows that that updaterec was called but your update statement didn't work. Most likely reason is that you ended up with no EndUserID. HTH.

        Comment

        Working...