Connecting Tech Pros Worldwide Forums | Help | Site Map

working with databases..

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 20 '09
guys i have a html form that has action to redirect it to asp page,, dat asp page has vb script that recieves values from the form and add it to access db..out of those i hav two textboxes for which i have to keep a constant value.. but when i execute it,,it gives me error..how can i do it without manually entering values for dose 2 fields.. i want vbscript here is what i have those two fields are itemid and price..

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">

<html>
<head>
</head>

<body>
<%
' Declaring variables
Dim name, email, country, itemcode,price, data_source, con, sql_insert

' A Function to check if some field entered by user is empty
Function ChkString(string)
If string = "" Then string = " "
ChkString = Replace(string, "'", "''")
End Function

' Receiving values from Form
name = ChkString(Request.Form("name"))
email = ChkString(Request.Form("email"))
country = ChkString(Request.Form("country"))
itemcode = ChkString(Request.Form("itemid"))
price = ChkString(Request.Form("price"))


data_source = "Provider=Microsoft.Jet.OLEDB.4.0; Data Source=" & _
Server.MapPath("form.mdb")
sql_insert = "insert into users (name, email, country, itemid, price) values ('" & _
name & "', '" & email & "', '" & country & "' , '" & itemid & "' , '" & price & "')"

' Creating Connection Object and opening the database
Set con = Server.CreateObject("ADODB.Connection")
con.Open data_source
con.Execute sql_insert
' Done. Close the connection
con.Close
Set con = Nothing
Response.Write "All records were successfully entered into the database."
%>



</body>
</html>

debasisdas's Avatar
Moderator
 
Join Date: Dec 2006
Location: Bangalore ,India
Posts: 7,511
#2: May 21 '09

re: working with databases..


you can simply hard code the values or use some constant .

Please post the exact error message.
prabirchoudhury's Avatar
Familiar Sight
 
Join Date: May 2009
Location: Wellington, New Zealand
Posts: 152
#3: May 22 '09

re: working with databases..


hey..next time try to wrap with [code] tag around your code please.. thanks..

1. you have to Set the Parameters values to to the parameters Collection and pass the string values.

2. you could replace name.Text with your scheck string value

Expand|Select|Wrap|Line Numbers
  1. Dim sql_insert As String = "INSERT INTO users (name, email, country, itemid, price) VALUES (@name, @email,@country,@itemid, @price )"
  2.         Dim cmd As New OleDb.OleDbCommand(sql_insert, data_source)
  3.         '   |||||   Set the Parameters values to to the parameters Collection
  4.         cmd.Parameters.AddWithValue("@name", name.Text)
  5.         cmd.Parameters.AddWithValue("@email", email.Text)
  6.         cmd.Parameters.AddWithValue("@country", country.Text)
  7.         cmd.Parameters.AddWithValue("@itemid", itemid.Text)
  8.         cmd.Parameters.AddWithValue("@price", price.Text)
  9.         conn.Open()
  10.         cmd.ExecuteNonQuery()
  11.         cmd.Dispose()
  12.         conn.Close()
  13.         lblResponse.Text = "Success!"
  14.  
  15.  
  16.  
  17.  
Newbie
 
Join Date: May 2009
Posts: 11
#4: Jul 21 '09

re: working with databases..


Hi to all,

Is there anyone know how to store images on a database, what are the procedures and also how to call? (citing an example would be appreciated) I know how to store data on a database like names, address, telephone number and other informations.. I used vb 6.0 and my backend is ms access.. anyone?! I need to know some of the concepts because I don't have any idea on it. very thanks if someone replies

=)
OuTCasT's Avatar
Needs Regular Fix
 
Join Date: Jan 2008
Location: South Africa
Posts: 361
#5: Jul 21 '09

re: working with databases..


Expand|Select|Wrap|Line Numbers
  1. function saveImage
  2.     <% 
  3.        Protected Sub Upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Upload.Click
  4.         Dim fileUpload1 As FileUpload = CType(Me.FindControl("fileUpload1"), FileUpload)
  5.         'Make sure a file has been successfully uploaded
  6.         If fileUpload1.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fileUpload1.PostedFile.FileName) OrElse fileUpload1.PostedFile.InputStream Is Nothing Then
  7.             Label1.Text = "Please Upload Valid picture file"
  8.             Exit Sub
  9.         End If
  10.         'Check that it is a JPG or GIFF file
  11.         Dim extension As String = System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName).ToLower()
  12.         Dim ImageType As String = Nothing
  13.          Select extension
  14.             Case ".gif"
  15.                 ImageType = "image/gif"
  16.             Case ".jpg", ".jpeg", ".jpe"
  17.                 ImageType = "image/jpeg"
  18.             Case ".png"
  19.                 ImageType = "image/png"
  20.             Case Else
  21.                 'Invalid file type uploaded
  22.                 Label1.Text = "Not a Valid file format"
  23.                 Exit Sub
  24.         End Select
  25.         'Connect to the database and insert a new record into Picture table
  26.         Dim myConnection As New SqlConnection("connectionString")
  27.         Const SQL As String = "INSERT INTO [Borrower] ([BorrowerPhoto]) VALUES (@ImageData)"
  28.         Dim myCommand As New SqlCommand(SQL, myConnection)
  29.  
  30.         'Load FileUpload's InputStream into Byte array
  31.         Dim imageBytes(fileUpload1.PostedFile.InputStream.Length) As Byte
  32.         fileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
  33.         myCommand.Parameters.AddWithValue("@ImageData", imageBytes)
  34.         myCommand.Parameters.AddWithValue("@Company", TextBox1.Text.Trim())
  35.         myConnection.Open()
  36.         myCommand.ExecuteNonQuery()
  37.         myConnection.Close()
  38.           End Sub
  39.     %>
  40.     end function
Reply