Simple Data Entry form using SQL database in ASP.Net

prayank23
 
on Feb 05, 2013 05:54 AM
5221 Views

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>
<asp:Content ID="BodyContent" runat="server" ContentPlaceHolderID="MainContent">
    <h2>
        welcome</h2>
<p>
        <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] = ?">
            <DeleteParameters>
                <asp:Parameter Name="name" Type="String" />
            </DeleteParameters>
            <InsertParameters>
                <asp:Parameter Name="name" Type="String" />
                <asp:Parameter Name="email" Type="String" />
                <asp:Parameter Name="phone" Type="Decimal" />
            </InsertParameters>
            <UpdateParameters>
                <asp:Parameter Name="email" Type="String" />
                <asp:Parameter Name="phone" Type="Decimal" />
                <asp:Parameter Name="name" Type="String" />
            </UpdateParameters>
        </asp:SqlDataSource>
    </p>
    <p>
        <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label1" runat="server" Text="Name"></asp:Label>
    </p>
    <p>
        <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label2" runat="server" Text="Email"></asp:Label>
    </p>
    <p>
        <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox>
&nbsp;<asp:Label ID="Label3" runat="server" Text="Phone"></asp:Label>
    </p>
    <p>
        <asp:Button ID="Button1" runat="server" Text="Submit"  />
    </p>
</asp:Content>

 

Download FREE API for Word, Excel and PDF in ASP.Net: Download
Mustafa
 
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
		connection.Open()
		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))
		command.ExecuteNonQuery()
		connection.Close()
	End Sub
End Class

and check whether it works