Click to See Complete Forum and Search --> : ASP to Database


archana_a
June 15th, 2002, 08:12 PM
How do I send data from an asp page to the database, i.e. insert rows.::(

johnmcTemp
June 16th, 2002, 08:11 PM
The basic idea is to use an ADO.NET connection object to connect to a database then form your SQL INSERT command with the appropriate ADO.NET "command" object.

Plenty of info about simple insertions into a database and connecting to one on MSDN and internet sites.

good luck.
- john

archana_a
June 16th, 2002, 11:10 PM
Thanks John,

I was able to find that information, however I havent been able to insert rows into the database. Surprisingly I am not even getting an error.

<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Data.SqlClient" %>
<%@ Import Namespace="System.Text"%>
<HTML>
<script language="VB" runat="server">
Dim MyConnection As SqlConnection

Sub Page_Load(Src As Object, e As EventArgs)
' Create a connection to the "pubs" SQL database located on
' the local computer.
myConnection = New SqlConnection("server=localhost;" &_
"database=smb01;User id = sa; Password = sa")
' Check whether this page is a postback. If it is not
' a postback, call a custom BindGrid function.
If Not IsPostBack Then
BindGrid()
End If
End Sub


Sub Add_Click(Sender As Object, e As EventArgs)
Dim myCommand As SqlCommand
Dim insertCmd As String
If (name.Value = "" ) Then
Message.InnerHtml = "ERROR: Null values not allowed for " _
& "Name"
Message.Style("color") = "red"
BindGrid()
Exit Sub
End If
' Build a SQL INSERT statement string for all the input-form
' field values.
insertCmd = "insert into CustomerWeb values ( @name, " +
"@address1, @city, @state, @zip)";
' Initialize the SqlCommand with the new SQL string.
myCommand = New SqlCommand(insertCmd, myConnection)
' Create new parameters for the SqlCommand object and
' initialize them to the input-form field values.


myCommand.Parameters("@name").Value = name.Value;
myCommand.Parameters.Add(New SqlParameter("name", _
SqlDbType.VarChar, 31))

myCommand.Parameters.Add(New SqlParameter("@address1", _
SqlDbType.VarChar, 31))
myCommand.Parameters("@address1").Value = address.Value;

myCommand.Parameters.Add(New SqlParameter("@City", _
SqlDbType.VarChar, 20))
myCommand.Parameters("@City").Value = city.Value;

myCommand.Parameters.Add(New SqlParameter("@State", _
SqlDbType.Char, 2))
myCommand.Parameters("@State").Value = state.Value;

myCommand.Parameters.Add(New SqlParameter("@Zip", _
SqlDbType.Char, 5))
myCommand.Parameters("@Zip").Value = zip.Value;

myCommand.Connection.Open()
' Test whether the new row can be added and display the
' appropriate message box to the user.


myCommand.Connection.Close()
BindGrid()
End Sub

' BindGrid connects to the database and implements a SQL
' SELECT query to get all the data in the "Authors" table
' of the database.
Sub BindGrid()
Dim myConnection As SqlConnection
Dim myCommand As SqlDataAdapter
' Create a connection to the "pubs" SQL database located on
' the local computer.
myConnection = New SqlConnection("server=localhost;" _
&amp; "database=sbm01;User Id = sa; Password = sa")
' Connect to the SQL database using a SQL SELECT query to get all
' the data from the "Customer Web" table.
myCommand = New SqlDataAdapter("SELECT * FROM CustomerWeb", _
myConnection)
' Create and fill a new DataSet.
Dim ds As DataSet = New DataSet()
myCommand.Fill(ds)
' Bind the DataGrid control to the DataSet.
MyDataGrid.DataSource = ds
MyDataGrid.DataBind()
End Sub
</script>
<body style="FONT: 10pt verdana">
<form runat="server">
<table width="95%">
<tr>
<td valign="top">
<ASP:DataGrid id="MyDataGrid" runat="server" Width="700" BackColor="White" BorderColor="black" ShowFooter="false" CellPadding="3" CellSpacing="0" Font-Name="Verdana" Font-Size="8pt" HeaderStyle-BackColor="#aaaadd" EnableViewState="false">
<HeaderStyle BackColor="#AAAADD"></HeaderStyle>
</ASP:DataGrid>
</td>
<td valign="top">
<table style="FONT: 8pt verdana">
<tr>
<td colspan="2" bgcolor="#ccccff">
Add a New Customer:</td>
</tr>
<tr>
<td nowrap>Name:
</td>
<td><input type="text" id="name" runat="server"></td>
</tr>
<tr>
<td>Address:
</td>
<td><input type="text" id="address1" runat="server"></td>
</tr>
<tr>
<td>City:
</td>
<td><input type="text" id="city" runat="server"></td>
</tr>
<tr>
<td>State:
</td>
<td>
<select id="state" runat="server">
<option selected>CA</option>
<option>IN</option>
<option>KS</option>
<option>MD</option>
<option>MI</option>
<option>OR</option>
<option>TN</option>
<option>UT</option>
</select>
</td>
</tr>
<tr>
<td nowrap>Zip Code:
</td>
<td><input type="text" id="zip" runat="server"></td>
</tr>
<tr>
<td></td>
<td>
<input type="submit" OnServerClick="Add_Click" value="Add Customer" runat="server" id="Submit1" name="Submit1">
</td>
</tr>
<tr>
<td colspan="2" align="middle">
<span id="Message" EnableViewState="false" runat="server"></span>
</td>
</tr>
</table>
</td>
</tr>
</table>
</form>
</body>
</HTML>


:confused: