working with databases.. | Newbie | | Join Date: May 2009
Posts: 1
| | |
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>
|  | Moderator | | Join Date: Dec 2006 Location: Bangalore ,India
Posts: 7,511
| | | re: working with databases..
you can simply hard code the values or use some constant .
Please post the exact error message.
|  | Familiar Sight | | Join Date: May 2009 Location: Wellington, New Zealand
Posts: 152
| | | 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 -
Dim sql_insert As String = "INSERT INTO users (name, email, country, itemid, price) VALUES (@name, @email,@country,@itemid, @price )"
-
Dim cmd As New OleDb.OleDbCommand(sql_insert, data_source)
-
' ||||| Set the Parameters values to to the parameters Collection
-
cmd.Parameters.AddWithValue("@name", name.Text)
-
cmd.Parameters.AddWithValue("@email", email.Text)
-
cmd.Parameters.AddWithValue("@country", country.Text)
-
cmd.Parameters.AddWithValue("@itemid", itemid.Text)
-
cmd.Parameters.AddWithValue("@price", price.Text)
-
conn.Open()
-
cmd.ExecuteNonQuery()
-
cmd.Dispose()
-
conn.Close()
-
lblResponse.Text = "Success!"
-
-
-
-
| | Newbie | | Join Date: May 2009
Posts: 11
| | | 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
=)
|  | Needs Regular Fix | | Join Date: Jan 2008 Location: South Africa
Posts: 361
| | | re: working with databases.. - function saveImage
-
<%
-
Protected Sub Upload_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Upload.Click
-
Dim fileUpload1 As FileUpload = CType(Me.FindControl("fileUpload1"), FileUpload)
-
'Make sure a file has been successfully uploaded
-
If fileUpload1.PostedFile Is Nothing OrElse String.IsNullOrEmpty(fileUpload1.PostedFile.FileName) OrElse fileUpload1.PostedFile.InputStream Is Nothing Then
-
Label1.Text = "Please Upload Valid picture file"
-
Exit Sub
-
End If
-
'Check that it is a JPG or GIFF file
-
Dim extension As String = System.IO.Path.GetExtension(fileUpload1.PostedFile.FileName).ToLower()
-
Dim ImageType As String = Nothing
-
Select extension
-
Case ".gif"
-
ImageType = "image/gif"
-
Case ".jpg", ".jpeg", ".jpe"
-
ImageType = "image/jpeg"
-
Case ".png"
-
ImageType = "image/png"
-
Case Else
-
'Invalid file type uploaded
-
Label1.Text = "Not a Valid file format"
-
Exit Sub
-
End Select
-
'Connect to the database and insert a new record into Picture table
-
Dim myConnection As New SqlConnection("connectionString")
-
Const SQL As String = "INSERT INTO [Borrower] ([BorrowerPhoto]) VALUES (@ImageData)"
-
Dim myCommand As New SqlCommand(SQL, myConnection)
-
-
'Load FileUpload's InputStream into Byte array
-
Dim imageBytes(fileUpload1.PostedFile.InputStream.Length) As Byte
-
fileUpload1.PostedFile.InputStream.Read(imageBytes, 0, imageBytes.Length)
-
myCommand.Parameters.AddWithValue("@ImageData", imageBytes)
-
myCommand.Parameters.AddWithValue("@Company", TextBox1.Text.Trim())
-
myConnection.Open()
-
myCommand.ExecuteNonQuery()
-
myConnection.Close()
-
End Sub
-
%>
-
end function
|  | Similar Visual Basic 4 / 5 / 6 bytes | | | /bytes/about
We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights.
Get the best answers to your questions from over 226,471 network members.
|