Asp.net VB Textbox to Sql

Ok I have visual Studio web developer and I have a database now what
it is some times frustrating to find custom SQL in Asp.net tutorials , you always get the same thing how to data bind , grid views ect but what if you just want to go from a text box control to a sql insert ? well it is pretty easy if you know where to start … in visual studio Web developer  first make a database table and include an id and set it as a primary key and a text field Varchar  lets call it “name” varchar (50) add some data to it then drag a good old fashion text box control and button on to a new Asp.net page then you will need to add a New  SqlDataSource on the page ,  connect to it then next then click advanced . and tick generate insert, update, Delete statements…and ok then finish now that you have all that click on the button to make an event handler

In VB.. Add this script to the event handler

SqlDataSource1.InsertParameters(“name”).DefaultValue = textbox1.Text.ToString()

SqlDataSource1.Insert()

DONE ! then you can see that your data has been inserted in to the table why not ad a grid view to test this below it
assuming you have an table and with id set to primary key and “name”
Example

Default.aspx:

<%@ Page Language=”VB” AutoEventWireup=”false” CodeFile=”again.aspx.vb” Inherits=”again” %>

<!DOCTYPE html PUBLIC “-//W3C//DTD XHTML 1.0 Transitional//EN” “http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd”>

<html xmlns=”http://www.w3.org/1999/xhtml”>

<head runat=”server”>
<title> </title>
</head>
<body>

<form id=”form1″ runat=”server”>

<div>

Name

<asp:TextBox ID=”TextBox1″ runat=”server”></asp:TextBox>

<asp:Button ID=”Button1″ runat=”server” Text=”Button” />

<br />

<asp:SqlDataSource ID=”SqlDataSource1″ runat=”server”

ConnectionString=”<%$ ConnectionStrings:customsiteConnectionString2 %>”

DeleteCommand=”DELETE FROM [mysimpletable] WHERE [id] = @id”

InsertCommand=”INSERT INTO [mysimpletable] ([name]) VALUES (@name)”

SelectCommand=”SELECT * FROM [mysimpletable]”

UpdateCommand=”UPDATE [mysimpletable] SET [name] = @name WHERE [id] = @id”>

<DeleteParameters>

<asp:Parameter Name=”id” Type=”Int32″ />

</DeleteParameters>

<InsertParameters>

<asp:Parameter Name=”name” Type=”String” />

</InsertParameters>

<UpdateParameters>

<asp:Parameter Name=”name” Type=”String” />

<asp:Parameter Name=”id” Type=”Int32″ />

</UpdateParameters>

</asp:SqlDataSource>

</div>

</form>

</body>

</html>

Default.Asp.VB:

Partial Class again

Inherits System.Web.UI.Page

Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles Button1.Click

SqlDataSource1.InsertParameters(“name”).DefaultValue = TextBox1.Text.ToString()

SqlDataSource1.Insert()

End Sub

End Class

and that is it not to bad

Comments are closed.

Subscription

Fill out the form below to signup to my newsletter and You Can Get New Cool free Scripts , advice ...ect

Our strict privacy policy keeps your email address 100% safe & secure.



























Ads1

Best Web Hosting
Web Hosting Supported by Experts.
50% OFF Hosting. Free $25 Adwords!
justhostrus.com





Accept Payment Online?
PayPal Merchant Account
Lets you Accept Secure Payment
www.PayPal.com/GetPaid





Cool Twitter Automation
The Best thing to happen
To Twitter EVER Free Ac!
www.tweetitlater.org






Godady Domain Name
Save Money On Domain Names
20% Of Domain Name Special!
www.Godady.com.au






Affiliate Tracking
Affiliate Tracking Software
Software Solutions for Every business
www.idevtracking.com





Designed by Taggart Jensen .