Simple Data Entry form using SQL database in ASP.Net

on Feb 05, 2013 05:54 AM

Basically I have put three text boxes for name,phone and email and a submit button on the design view of the default.aspx......

Now I have tried to attach SQL database by different methods but nothing is working....

I only want that I am having a Sql datbase with same name tables and the form data should get submitted there. So what is the INSERT code I need to write in the COde section and please explain the method fully as how and what SQL controls to use and how to use MS sql server for that. Please I am a newbie in asp and need to know this for my Engineering final year project.

Thanks in advance

Below is the markup code for the default.aspx


<%@ Page Title="Home Page" Language="VB" MasterPageFile="~/Site.Master" AutoEventWireup="false"
    CodeFile="Default.aspx.vb" Inherits="_Default" %>

<asp:Content ID="HeaderContent" runat="server" ContentPlaceHolderID="HeadContent">
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
        <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
            ConnectionString="<%$ ConnectionStrings:ConnectionString %>" 
            DeleteCommand="DELETE FROM [users] WHERE [name] = ?" 
            InsertCommand="INSERT INTO [users] ([name], [email], [phone]) VALUES (?,?,?)" 
            ProviderName="<%$ ConnectionStrings:ConnectionString.ProviderName %>" 
            SelectCommand="SELECT [name], [email], [phone] FROM [users]" 
            UpdateCommand="UPDATE [users] SET [email] = ?, [phone] = ? WHERE [name] = ?">
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="email" Type="String" />
                <asp:Parameter Name="phone" Type="Decimal" />
                <asp:Parameter Name="email" Type="String" />
                <asp:Parameter Name="phone" Type="Decimal" />
                <asp:Parameter Name="name" Type="String" />
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label2" runat="server" Text="Email"></asp:Label>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label>
        <asp:Button ID="Button1" runat="server" Text="Submit"  />


Download FREE API for Word, Excel and PDF in ASP.Net: Download
on Feb 05, 2013 07:34 AM
on Feb 05, 2013 09:53 AM

Your design is proper only you have to do is you will have to replace your button part in your markup code with this

  <asp:Button ID="Button1" runat="server" Text="Submit" OnClick="OnSubmitClick" />

Now you will have to write this code in default.cs

Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports System.Web
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Data
Imports System.Data.SqlClient
Imports System.Configuration

Public Partial Class _Default
	Inherits System.Web.UI.Page
	Protected Sub Page_Load(sender As Object, e As EventArgs)

	End Sub

	Protected Sub OnSubmitClick(sender As Object, e As EventArgs)
		Dim conString As String = ConfigurationManager.ConnectionStrings("constr").ConnectionString
		Dim connection As New SqlConnection()
		connection.ConnectionString = conString
		Dim command As New SqlCommand("INSERT INTO [users] ([name], [email], [phone]) VALUES (@n,@e,@p)", connection)
		command.Parameters.Add(New SqlParameter("@n", TextBox1.Text))
		command.Parameters.Add(New SqlParameter("@e", TextBox2.Text))
		command.Parameters.Add(New SqlParameter("@p", TextBox3.Text))
	End Sub
End Class

and check whether it works