0

Hi daniweb

first of all, sry my english.

I have a problem with at update code, I was hoping that U can help me, im new to this.
i have a code where i get an id from another page, from update.aspx?id=1 up to 6 ATM.
my code works fine to get the record from my DB, from the id number (in this case 1)
But when i then add my buttom.Click code, i get some not declared errors !?
its the addwithvalue lines.

Hope someone can help me and maybe guide me to a better solution on how to show the text, right now i show one record in a repeater.

Main site code.

<%@ Page Language="VB" AutoEventWireup="false" CodeFile="Updateitems.aspx.vb" Inherits="Updateitems" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
    



    <asp:Repeater ID="Repeater2" runat="server">
    <HeaderTemplate>Start på Recorder Update<br /></HeaderTemplate>
    <ItemTemplate>
        <asp:TextBox ID="upd_thetitle" runat="server" text='<%# Container.DataItem("the_title")%>'></asp:TextBox>
        <asp:TextBox ID="upd_thetext" runat="server" Text='<%# Container.DataItem("the_text")%>'></asp:TextBox>
        <asp:TextBox ID="upd_thenumber" runat="server" Text='<%# Container.DataItem("the_number")%>'></asp:TextBox>
        <asp:DropDownList ID="upd_thekat" runat="server">
        <asp:ListItem Value="1">Kat1</asp:ListItem>
        <asp:ListItem Value="2">Kat2</asp:ListItem>
        <asp:ListItem Value="3">Kat3</asp:ListItem>
        <asp:ListItem Value="4">Kat4</asp:ListItem>
        </asp:DropDownList>
        <asp:Button ID="Button2" runat="server" Text="Updater" OnClick="Button2_Click" /></div>
    </ItemTemplate>
    <FooterTemplate>Slut på Recorder Update</FooterTemplate>
    </asp:Repeater>
    </div>
    <br />
        <br />
        <div>
                    <asp:Label ID="lblId" runat="server" Text=""></asp:Label>
    </div>
    </form>
</body>
</html>

Codebehind.

Imports System.Data
Imports System.Data.OleDb
Imports System.IO

Partial Class Updateitems
    Inherits System.Web.UI.Page

    ' Access Database oledb connection string
    ' Using Provider Microsoft.Jet.OLEDB.4.0
    Dim connStr As String = "Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("db/DBmaster.mdb")

    ' Object created for Oledb Connection
    Dim myAccessConnection As New OleDbConnection(connStr)
    ' postback ID for update
    Dim updId As String = Context.Request.QueryString("id")
    Dim upd_thekat As Object

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        Try
            openAccessConnection()

            Dim cmd As New OleDbCommand("select * from TBL where the_id =" & updId & "", myAccessConnection)

            ' Mark the Command as a Text
            cmd.CommandType = CommandType.Text

            ' Add Parameters to Command

            Dim myAdapter As New OleDbDataAdapter(cmd)
            Dim myDataSet As New DataSet
            myAdapter.Fill(myDataSet)

            Repeater2.DataSource = myDataSet
            DataBind()

            closeAccessConnection()

        Catch exc As Exception
            closeAccessConnection()

        End Try
    End Sub

    Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
        Try
            openAccessConnection()

            Dim cmd As New OleDbCommand("update TBL set the_title = ?,the_text = ?,the_number = ?,the_kat = ? where the_id =" & updId & "", myAccessConnection)
            cmd.CommandType = CommandType.Text
            'UpdateCommand = cmd

            cmd.Parameters.AddWithValue("@thetitle", OleDbType.VarChar).Value = upd_thetitle.Text
            cmd.Parameters.AddWithValue("@thetext", OleDbType.VarChar).Value = upd_thetext.Text
            cmd.Parameters.AddWithValue("@thenumber", OleDbType.Integer).Value = upd_thenumber.Text
            cmd.Parameters.AddWithValue("@thekat", OleDbType.VarChar).Value = upd_thekat.Text


            Dim result As Integer
            result = cmd.ExecuteNonQuery()
            If (result > 0) Then
                lblId.Text = "success med at updater denne record"
            Else
                lblId.Text = "Error"
            End If
            closeAccessConnection()

        Catch ex As Exception
            Response.Write(ex.Message)
            closeAccessConnection()
        End Try

        'Response.Redirect("default.aspx")
    End Sub

    Public Sub openAccessConnection()
        ' If condition that can be used to check the access database connection
        ' whether it is already open or not.
        If myAccessConnection.State = ConnectionState.Closed Then
            myAccessConnection.Open()
        End If
    End Sub

    Public Sub closeAccessConnection()
        ' If condition to check the access database connection state
        ' If it is open then close it.
        If myAccessConnection.State = ConnectionState.Open Then
            myAccessConnection.Close()
        End If
    End Sub

End Class

hope someone can help me, the errors i get is for:
cmd.Parameters.AddWithValue("@thetitle", OleDbType.VarChar).Value = upd_thetitle.Text
cmd.Parameters.AddWithValue("@thetext", OleDbType.VarChar).Value = upd_thetext.Text
cmd.Parameters.AddWithValue("@thenumber", OleDbType.Integer).Value = upd_thenumber.Text
cmd.Parameters.AddWithValue("@thekat", OleDbType.VarChar).Value = upd_thekat.Text
Where it say that upd_thexxxxxxxx is not declared

and
Protected Sub Button2_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button2.Click
here i get a required a withevents...

2
Contributors
1
Reply
2
Views
5 Years
Discussion Span
Last Post by Sarama2030
0

Sql strings are surrounded with single quotes like 'parameter value'.Use single quotes and see what happens

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.