Update statement

collie

New member
Update statement

Hi, Can someone please help me? I am trying to update my database with the values entered in the textbox. I am not using a datagrid. This is my code so far: Imports System.Data Imports System.Data.OleDb Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here If Not Page.IsPostBack Then Dim ad_id As Integer ad_id = Session("ad_id") Response.Write(ad_id) TxtTitle.Text = Request.QueryString("textboxvalue") lblID.Text = ad_id Dim con As New ADODB.Connection con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/duclassified.mdb")) Dim rs1 As New ADODB.Recordset rs1.Open("Select * from ads where ad_id=" & ad_id, con) Dim ad_title As String Dim ad_price 'ad_title = rs1("ad_title").Value 'TxtTitle.Text = ad_title ad_price = rs1("ad_price").Value TxtPrice.Text = ad_price rs1.Close() rs1 = Nothing End Sub Function Update() Dim ad_id = lblID.Text Dim cn1 As New ADODB.Connection() cn1.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/duclassified.mdb")) Dim rs1 As New ADODB.Recordset() Dim cmd As New OleDbCommand("UPDATE users SET ad_title=' " & TxtTitle.Text & "' WHERE ad_ID=" & ad_id & " ", cn1) cmd.ExecuteNonQuery() rs1.Close() rs1 = Nothing End Function Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click call update () end sub
 

yuval k

New member
?Well, what is the question

Please write what the problem you encounter is: • Doesn't the database get updated? • Do you get any error code? Reading a lot of code isn't a very pleasant task..
An article about working with ADODB in C#, just in case
By the way, if you prefer English, you can try this version of the forums
 

gilad g

New member
assumption

It's possible that there are no records in the DB, that match the condition given by the WHERE clause. Check to see that such records exist
 

collie

New member
Hi

thanks for the quick response. The record does exist. I used a recordset as i haven't the foggiest idea what to do
And thanks for letting me know about this version of the forum. it's great.
 

yuval k

New member
You welcome ../images/Emo140.gif

Anyway, can you please align the message content to the left (like you did, with h-code and s-code)?
 

collie

New member
../images/Emo9.gif i have a new problem

Forget about my previous emails as i have a new urgent problem. I am having trouble with my application and don't understand why. When I click the update button the Title field in my database is supposed to get updated according to the value that i entered in txtTtitle. However when i debug the application i see that It sets the txtTitle.text to nothing. the Title field in my db then becomes empty. when i enter values into (cmd.CommandText = "update ads set ad_title='" & ad_title & "' where ad_id=" & ad_id.ToString) instead of ad_title and ad_id then everything works fine. however, that is not the idea. i need to update the fields according to what the user entered. What am i missing? Must i change something in the textbox properties? Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load 'Put user code to initialize the page here Dim ad_id As Integer lblID.Text = Request.QueryString("ad_id") Dim con As New ADODB.Connection con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/database/duclassified.mdb")) Dim rs1 As New ADODB.Recordset rs1.Open("Select * from ads where ad_id=3059", con) Dim ad_title As String Dim ad_price ad_title = rs1("ad_title").Value TxtTitle.Text = ad_title ad_price = rs1("ad_price").Value TxtPrice.Text = ad_price rs1.Close() rs1 = Nothing con.Close() End Sub Function Update() ' If Not Page.IsPostBack Then Dim ad_id As Integer = Me.lblID.Text Dim ad_title As String = Request.QueryString(Me.TxtTitle.Text) Dim cmd As New ADODB.Command Dim mycon As New ADODB.Connection mycon.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/database/duclassified.mdb")) cmd.CommandText = "update ads set ad_title='" & ad_title & "' where ad_id=" & ad_id.ToString cmd.ActiveConnection = mycon cmd.Execute() End Function Private Sub btnUpdate_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUpdate.Click Update() End Sub End Class
 

yuval k

New member
Don't you get any excpetions here?

You declare ad_id as an integer, however you assign a string value into it... Dim ad_id As Integer = Me.lblID.Text Can you try to check what the SQL string of query is?​
 

collie

New member
no exceptions

there is no problem with the ad_id. it recognises it. However now the problem with the txtTitle.text is that when i change the value and i debug the application i see that it doesn't recognise the new value. it only recognises the original value and therefore doesn't perform the update. Why?
 

gilad g

New member
It's because..

you are getting it from the QueryString, rather than directly calling it. Remove the Request.QueryString call, and leave the rest, like so: Dim ad_title As String = Me.TxtTitle.Text The Textbox's value is NOT passed by the querystring, but is saved in the ViewState (which is an encrypted POST value). One more thing -- I DO NOT recommend using the Recordset, as it is slow (and rather old
), consider using the DataReader instead.​
 

collie

New member
i solved the problem

First of all i thank you for all your kind and patient help (also Yuvalk). I am not using datareader as my job doesn't want to use it yet. they are making life very hard for me as i don't have experience and all the books and other reading material use ADO.NET and Datareader, datagrid etc. To complete what they want me to do i am also working at home. oh well at least i have a job. I am having another problem though but i will post it later on. I think just now you will have to open a new forum just for me with all my problems
Anyway, here is the working code for anyone that wants. All i did was include the textbox values in postback block: If Not Page.IsPostBack Then Dim cn As New ADODB.Connection cn.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/duclassified.mdb")) Dim rs As New ADODB.Recordset rs.Open("Select * from cats ORDER BY CAT_DATED DESC", cn) Dim ad_id As Integer lblID.Text = Request.QueryString("ad_id") Dim con As New ADODB.Connection con.Open("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=" & Server.MapPath("/duclassified.mdb")) Dim rs1 As New ADODB.Recordset rs1.Open("Select * from ads where ad_id=3059", con) Dim ad_title As String Dim ad_price As String Dim ad_description As String Dim ad_qty As Integer Dim ad_link As String Dim ad_picture As String ad_title = rs1("ad_title").Value ad_qty = rs1("AD_QUANTITY").Value ad_price = rs1("ad_price").Value ad_link = rs1("ad_link").Value ad_picture = rs1("AD_IMAGE").Value ad_description = rs1("AD_DESCRIPTION").Value TxtTitle.Text = ad_title txtQuantity.Text = ad_qty TxtPrice.Text = ad_price TxtLink.Text = ad_link TxtPic.Text = ad_picture txtDescription.Text = ad_description rs1.Close() rs1 = Nothing con.Close() End If End Sub
 

yuval k

New member
Great ../images/Emo13.gif We were glad to help you

כולם מוזמנים לשאול את שאלותיהם... לא הביישן למד, וזהו המקום
 

collie

New member
I have 2 questions

1. Where can I buy English books about vb.net? Not over the Internet as I want to read abit to see that the book suits me. 2.Cookies In my asp page i have the following code: <% If Not Request.Form("Name") = "" Then Response.Cookies("Name") = Request.Form("Name") end if %> <html> In my aspx page i have the following: Public Class COOKIES Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load If Not Page.IsPostBack Then Dim c As New System.Web.HttpCookie("Name", "my cookie value") c = Request.Cookies("Name") If Not (c Is Nothing) Then Response.Write(c) Else Response.Write("username can't be found") End If However the aspx doesn't seem to recognise the cookie. it returns username can't be found. why? Thanks
 

gilad g

New member
I'll try to answer...

1. Personally, i don't have alot (read: any) experience with .net books in English. However, word is, that Wrox's books are pretty good
2. Try this (might work, might not... i'm just guessing here...) Dim c As System.Web.HttpCookie c = Request.Cookies("Name")​
 

collie

New member
Cookies

In my asp page i have the following code: <% If Not Request.Form("Name") = "" Then Response.Cookies("Name") = Request.Form("Name") end if %> <html> In my aspx page i have the following: Public Class COOKIES Inherits System.Web.UI.Page Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Dim NAME NAME = Request.Cookies("Name") Response.Write(NAME) However the aspx doesn't seem to recognise the cookie. why?
 

collie

New member
Reading from ASP to ASPX

Hi, I need to retrieve data from an ASP page to ASPX. How do I do that?
 

yuval k

New member
../images/Emo18.gif נא שרשרי שאלותיך.

Well, that depends on the type of the data you want to transfer between the pages... One method is through the QueryString, another involvs using one of the solutions for sharing session variables between ASP and ASP.net pages.​
 

collie

New member
I need to pass mostly strings

from a table. The querystring that is sent from the asp page looks like this when i press the submit button: http://localhost/editAd.asp?ad_id=3059&cat_id=8&sub_id=25 If I can get the AD_ID then I will be fine. Must the 2 pages reside in the same directory for this to work? How exactly will i request the ad_id on the aspx page?
 
למעלה