working with databases..

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • imruler
    New Member
    • May 2009
    • 1

    working with databases..

    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(strin g)
    If string = "" Then string = " "
    ChkString = Replace(string, "'", "''")
    End Function

    ' Receiving values from Form
    name = ChkString(Reque st.Form("name") )
    email = ChkString(Reque st.Form("email" ))
    country = ChkString(Reque st.Form("countr y"))
    itemcode = ChkString(Reque st.Form("itemid "))
    price = ChkString(Reque st.Form("price" ))


    data_source = "Provider=Micro soft.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.CreateOb ject("ADODB.Con nection")
    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
    Recognized Expert Expert
    • Dec 2006
    • 8119

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

    Please post the exact error message.

    Comment

    • prabirchoudhury
      New Member
      • May 2009
      • 162

      #3
      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

      Code:
      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!"

      Comment

      • Kyosuke18
        New Member
        • May 2009
        • 11

        #4
        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

        =)

        Comment

        • OuTCasT
          Contributor
          • Jan 2008
          • 374

          #5
          Code:
          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

          Comment

          Working...