Solved

ASP.NET database help.

Posted on 2004-08-31
9
338 Views
Last Modified: 2006-11-17
all new to asp this is what i have so far, need to write data from a form into my new database. can someone help, any code example i can add to this, just something simple like wrting something to the database.

could you please use my code below as it make it easy for some on new like me.

<%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
      <script language="VB" runat="server">
      Dim nRecCount As Integer
      Dim Query As String = "select * from members"
      Dim dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("cl.mdb"))
      Dim mySelectCommand as OleDbCommand = New OleDbCommand(Query, dbconn)
    Dim myOleDbDataAdapter as OleDbDataAdapter = new OleDbDataAdapter(mySelectCommand)
      dbconn.Close()
      </script>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

</body>
</html>
0
Comment
Question by:pigmentarts
  • 5
  • 4
9 Comments
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11941002
here ya go:

<TABLE cellSpacing="0" cellPadding="5" width="550" border="0">
                                          <TR>
                                                <TD class="PageHeader" style="HEIGHT: 24px" colSpan="2">Add New Product</TD>
                                          </TR>
                                          <TR>
                                                <TD width="192">Product&nbsp;Name</TD>
                                                <TD width="338"><asp:textbox id="ProductName" runat="server" CssClass="ThinInput" Columns="15" MaxLength="50"></asp:textbox>&nbsp;&nbsp;
                                                      <asp:requiredfieldvalidator id="ProductNameValidator" runat="server" Enabled="true" EnableClientScript="False"
                                                            ControlToValidate="ProductName" ErrorMessage="Please enter a name."></asp:requiredfieldvalidator></TD>
                                          </TR>
                                          <TR>
                                                <TD style="HEIGHT: 15px" width="192">Tier1</TD>
                                                <TD style="HEIGHT: 15px" width="338"><asp:dropdownlist id="Tier1" runat="server" Width="150px" AutoPostBack="True"></asp:dropdownlist>&nbsp;
                                                      <asp:requiredfieldvalidator id="Tier1Validator" runat="server" Enabled="true" EnableClientScript="False" ControlToValidate="ProductName"
                                                            ErrorMessage="Please select a tier1."></asp:requiredfieldvalidator></TD>
                                          </TR>
                                          <TR>
                                                <TD width="192">Tier2&nbsp; <SPAN class="SmallText">(optional)</SPAN></TD>
                                                <TD width="338"><asp:dropdownlist id="Tier2" runat="server" Width="150px" AutoPostBack="True"></asp:dropdownlist></TD>
                                          </TR>
                                          <TR>
                                                <TD width="192">Tier3&nbsp; <SPAN class="SmallText">(optional)</SPAN></TD>
                                                <TD width="338"><asp:dropdownlist id="Tier3" runat="server" Width="150px"></asp:dropdownlist></TD>
                                          </TR>
                                          <TR>
                                                <TD vAlign="top">Description <SPAN class="SmallText">(optional)</SPAN></TD>
                                                <TD><asp:textbox id="Description" runat="server" CssClass="ThinInput" Columns="35" MaxLength="255"
                                                            TextMode="MultiLine" Rows="3"></asp:textbox></TD>
                                          </TR>
                                          <TR>
                                                <TD style="HEIGHT: 34px">Picture <SPAN class="SmallText">(optional)</SPAN></TD>
                                                <TD style="HEIGHT: 34px"><INPUT class="ThinInput" id="ImageSrc" type="file" size="15" runat="server"></TD>
                                          </TR>
                                          <TR>
                                                <TD style="HEIGHT: 34px">Price <SPAN class="SmallText">(optional)</SPAN></TD>
                                                <TD style="HEIGHT: 34px"><asp:textbox id="Price" runat="server" CssClass="ThinInput" Columns="4" MaxLength="7"></asp:textbox></TD>
                                          </TR>
                                          <TR>
                                                <TD align="right" colSpan="2"><asp:button id="AddProduct" runat="server" CssClass="ThinButton" Width="98px" CausesValidation="False"
                                                            Text="Add Product"></asp:button></TD>
                                          </TR>
                                          <TR>
                                                <TD colSpan="2"><asp:label id="StatusLabel" runat="server"></asp:label></TD>
                                          </TR>
                                    </TABLE>

Region " AddProduct Button "
    Private Sub AddProduct_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles AddProduct.Click
        If IsRefresh Then Return

        ProductNameValidator.Enabled = True
        Tier1Validator.Enabled = True
        QueryValidator.Enabled = False
        Validate()

        If Not IsValid Then Return

        Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New OleDb.OleDbCommand
        Dim IncomingFile As IO.FileInfo
        Dim ImageSource, PriceValue As String
        Dim ImageResizer As New ResizeImage(True, 300, 400)

        ImageResizer.ThumbX = 100
        ImageResizer.ThumbY = 100

        If ImageSrc.PostedFile.ContentLength <> 0 Then
            IncomingFile = New IO.FileInfo(Request.Files(0).FileName)
            IncomingFile = New IO.FileInfo(Server.MapPath("~/Pictures/Products/") & IncomingFile.Name)
            Request.Files(0).SaveAs(IncomingFile.FullName)
            ImageSource = ImageResizer.ResizeImage(IncomingFile.FullName)
        End If


        If Price.Text = "" Then
            PriceValue = "0"
        Else
            PriceValue = Price.Text
        End If

        cmd.CommandType = CommandType.Text
        cmd.CommandText = "INSERT INTO Products (Tier1ID, Tier2ID, Tier3ID, Name, " & _
            "Description, ImageSrc, Price) VALUES (" & Tier1.SelectedValue & ", " & _
            Tier2.SelectedValue & ", " & Tier3.SelectedValue & ", '" & DBSafe(ProductName.Text) & _
            "', '" & DBSafe(Description.Text) & "', '" & ImageSource & "', " & PriceValue & ")"
        cmd.Connection = cnn

        cnn.Open()
        cmd.ExecuteNonQuery()
        cnn.Close()

        cmd.Dispose()
        cnn.Dispose()

        LoadProductData()
    End Sub
#End Region

Let me know if you need any more help

Regards,

Aeros
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11941010
I define my connection string in my web.config, like so:

<appSettings>
            <add key="SiteDB" value="Provider=Microsoft.Jet.OleDb.4.0;Data Source=C:\Inetpub\wwwroot\DOTNETShoppingCart\Database\Site.mdb;"></add>
</appSettings>
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 11941098
this is what i have, i need to write the data in to my database called cl.mdb the form, AerosSaga the code looks good but this is my first time with asp so i really want my code below to work so i can go though it. also i am getting a erro alsor...

Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.

Compiler Error Message: BC30188: Declaration expected.

Source Error:

 

Line 10:       Dim mySelectCommand as OleDbCommand = New OleDbCommand(Query, dbconn)
Line 11:     Dim myOleDbDataAdapter as OleDbDataAdapter = new OleDbDataAdapter(mySelectCommand)
Line 12:       dbconn.Close()
Line 13:       </script>
Line 14: <head>
 

-------------------
my code below: at the moment is does nothing just bring a error, what i want is it to write the stuff from the form to my database
-----------------------


 <%@ Page Language="VB" ContentType="text/html" ResponseEncoding="iso-8859-1" %>
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.OleDb" %>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
      <script language="VB" runat="server">
      Dim nRecCount As Integer
      Dim Query As String = "select * from members"
      Dim dbconn=New OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0; data source=" & server.mappath("cl.mdb"))
      Dim mySelectCommand as OleDbCommand = New OleDbCommand(Query, dbconn)
    Dim myOleDbDataAdapter as OleDbDataAdapter = new OleDbDataAdapter(mySelectCommand)
      dbconn.Close()
      </script>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
</head>
<body>

 <form runat="server">
 
 <input type="text" id="title" value="Title" runat="server"><br>
 <input type="text" id="first_name" value="First Name" runat="server"><br>
 <input type="text" id="last_name" value="Last Name" runat="server"><br>
 <input type="text" id="company_name" value="Company Name" runat="server"><br>
 <input type="text" id="number" value="House Number" runat="server"><br>
 <input type="text" id="street" value="Street" runat="server"><br>
 <input type="text" id="town" value="Town" runat="server"><br>
 <input type="text" id="postcode" value="Postcode" runat="server"><br>
 <input type="text" id="telephone" value="Telephone" runat="server"><br>
 <input type="text" id="fax" value="Fax" runat="server"><br>
 <input type="text" id="email" value="Email" runat="server"><br>
 <input type="text" id="password" value="Password" runat="server"><br>
 <input type="text" id="web_address" value="Web Address" runat="server"><br>
 <select id="sector" runat="server"><br>
                  <option>Automotive</option>
                  <option>Computers</option>  
                  <option>Construction</option>  
                  <option>Consumer Goods</option>  
                  <option>Education</option>  
                  <option>Electronics</option>
                  <option>Entertainment</option>  
                  <option>Financial Institution</option>  
                          <option>Insurance</option>  
                          <option>Government</option>  
                          <option>Military</option>  
                          <option>Public Sector</option>  
                          <option>Hospitality</option>  
                          <option>Recreation</option>  
                          <option>Leisure Industry</option>  
                          <option>Manugacturer</option>  
                          <option>Media</option>
                          <option>Medical</option>
                          <option>Health Services</option>
                          <option>Pharmaceuticals</option>
                          <option>Professional Services</option>
                          <option>Publishers</option>
                          <option>Retail</option>
                          <option>Telecommunications</option>
                          <option>Travel</option>
                          <option>Transportation</option>
                          <option>Other</option>
                         
 </select><br>
 <input type="submit" OnServerClick="add_new_member" value="Submit" runat="server"><br>
 
 </form>



</body>
</html>
0
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 17

Expert Comment

by:AerosSaga
ID: 11941148
Ok first thing if you want to perform a forward only type action such as writing to the database you would want to make it a datareader instead of a dataadapter, much more efficient, however you have a select command not an insert or update command so I guess the first question is what do you really want to do select for display in a datagrid, datalist, repeater or do you want to update your db with information from a form?  Let me know and we'll go from there.

Aeros

0
 
LVL 12

Author Comment

by:pigmentarts
ID: 11941231
sorry about this, I was just having a go, don’t really know what I am doing yet, but I am willing to learn by example, maybe if i explain better you can help

I have a database called cl.mdb, in there database is empty fields called title, name etc.

I need a form that the user can enter there details in like the one bellow

then once submited all this information gets put into my database. I have been told that I need to do it in asp.net vb no c# so they can change later if they like.

any example, and thank you in advanced


<form runat="server">
 
 <input type="text" id="title" value="Title" runat="server"><br>
 <input type="text" id="first_name" value="First Name" runat="server"><br>
 <input type="text" id="last_name" value="Last Name" runat="server"><br>
 <input type="text" id="company_name" value="Company Name" runat="server"><br>
 <input type="text" id="number" value="House Number" runat="server"><br>
 <input type="text" id="street" value="Street" runat="server"><br>
 <input type="text" id="town" value="Town" runat="server"><br>
 <input type="text" id="postcode" value="Postcode" runat="server"><br>
 <input type="text" id="telephone" value="Telephone" runat="server"><br>
 <input type="text" id="fax" value="Fax" runat="server"><br>
 <input type="text" id="email" value="Email" runat="server"><br>
 <input type="text" id="password" value="Password" runat="server"><br>
 <input type="text" id="web_address" value="Web Address" runat="server"><br>
 <select id="sector" runat="server"><br>
                  <option>Automotive</option>
                  <option>Computers</option>  
                  <option>Construction</option>  
                  <option>Consumer Goods</option>  
                  <option>Education</option>  
                  <option>Electronics</option>
                  <option>Entertainment</option>  
                  <option>Financial Institution</option>  
                          <option>Insurance</option>  
                          <option>Government</option>  
                          <option>Military</option>  
                          <option>Public Sector</option>  
                          <option>Hospitality</option>  
                          <option>Recreation</option>  
                          <option>Leisure Industry</option>  
                          <option>Manugacturer</option>  
                          <option>Media</option>
                          <option>Medical</option>
                          <option>Health Services</option>
                          <option>Pharmaceuticals</option>
                          <option>Professional Services</option>
                          <option>Publishers</option>
                          <option>Retail</option>
                          <option>Telecommunications</option>
                          <option>Travel</option>
                          <option>Transportation</option>
                          <option>Other</option>
                         
 </select><br>
 <input type="submit" OnServerClick="add_new_member" value="Submit" runat="server"><br>
 
 </form>


0
 
LVL 12

Author Comment

by:pigmentarts
ID: 11941247
ps i may also want a page after to confirm details on screen if you knwo what i mean
0
 
LVL 17

Accepted Solution

by:
AerosSaga earned 500 total points
ID: 11941299
This is about as simple as it gets:

Dim cnn As New OleDb.OleDbConnection(ConfigurationSettings.AppSettings("SiteDB"))
        Dim cmd As New OleDb.OleDbCommand
        Dim IncomingFile As IO.FileInfo

 If ImageSrc.PostedFile.ContentLength <> 0 Then ****You only need this if you are uploading pictures or files
            IncomingFile = New IO.FileInfo(Request.Files(0).FileName)
            IncomingFile = New IO.FileInfo(Server.MapPath("~/Pictures/Products/") & IncomingFile.Name)
            Request.Files(0).SaveAs(IncomingFile.FullName)
        End If

 cmd.CommandType = CommandType.Text  ****Substitute your textbox values for the ones I have here
        cmd.CommandText = "INSERT INTO Products (Tier1ID, Tier2ID, Tier3ID, Name, " & _
            "Description, ImageSrc, Price) VALUES (" & Tier1.SelectedValue & ", " & _
            Tier2.SelectedValue & ", " & Tier3.SelectedValue & ", '" & DBSafe(ProductName.Text) & _
            "', '" & DBSafe(Description.Text) & "', '" & ImageSource & "', " & PriceValue & ")"
        cmd.Connection = cnn

        cnn.Open()
        cmd.ExecuteNonQuery()
        cnn.Close()

        cmd.Dispose()
        cnn.Dispose()

Thats it and you just updated your db from a user populated form, and with a picture if you wanted ;)
0
 
LVL 12

Author Comment

by:pigmentarts
ID: 11941314
thank you very much. been a good help, may need help later will post again. take care
0
 
LVL 17

Expert Comment

by:AerosSaga
ID: 11941341
your very welcome glad I could be of service

Aeros
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
ASP.Net to Oracle Connectivity Recently I had to develop an ASP.NET application connecting to an Oracle database.As I am doing it first time ,I had to solve several problems. This article will help to such developers  to develop an ASP.NET client…
Attackers love to prey on accounts that have privileges. Reducing privileged accounts and protecting privileged accounts therefore is paramount. Users, groups, and service accounts need to be protected to help protect the entire Active Directory …

730 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question