HOME AJAX VALIDATION EMAIL ADO.NET SILVERLIGHT CONTACT US |
We can use ADO.NET with insert queries to insert data directly to the SQL SERVER DATABSE. All you need is a connection between your visual studio and database server. But if you don't know how to do it, relax... and simply read and this tutorial.
Create a New website. >> select an Empty website. click OK.
Now, First thing we want to do is design a GUI that can enter the data into the database table. (assuming that the database table is already created).
Go to the default.aspx designer and insert following things:
- "5 textboxes and labels each for Name , username, email, password and confirm password. respectively."
- 1 asp button to submit the data.
Here is the design exaple:
<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>
<br />
<br />
Name:
<asp:TextBox ID="TextBox1" runat="server" BackColor="#999966" Height="32px"
Width="245px"></asp:TextBox>
<br />
UserName: <asp:TextBox ID="TextBox2" runat="server" BackColor="#999966" Height="32px"
Width="245px"></asp:TextBox>
<br />
Email <asp:TextBox ID="TextBox3" runat="server" BackColor="#999966" Height="32px"
Width="245px"></asp:TextBox>
<br />
Password <asp:TextBox ID="TextBox4" runat="server" BackColor="#999966" Height="32px"
Width="245px"></asp:TextBox>
<br />
ConConfirm Password
<asp:TextBox ID="TextBox5" runat="server" BackColor="#999966" Height="32px"
Width="245px"></asp:TextBox>
<br />
<br />
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Submit" />
<asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
<br />
</div>
</form>
</body>
</html>
After Doing all the above things your design page may look some what like this.
Now , After GUI, next step is the code behind the page.
Goto the Defsult .aspx.cs page .
First add the Namespace to this page which is very important to use sql elements.
add> System.Data.SqlClient in the namespaces at the top of page.
Go to the graphics designer page and double click the submit button. The click event of button will be shown. Inside it write the following code.
for example-
{
string s = "<CONNECTION STRING OF UR DATABASE>";
SqlConnection c = new SqlConnection(s);
try
{
SqlCommand cmd = new SqlCommand(" INSERT INTO <TABLE NAME> (<COLOUMN1 NAME >,<COLOUMN2 NAME >,<COLOUMN3 NAME >,<COLOUMN4 NAME >,<COLOUMN5 NAME >) VALUES ('"+TextBox1.Text+"','"+TextBox2.Text+"','"+TextBox3.Text+"','"+TextBox4.Text+"','"+TextBox5.Text+"' " ,c );
// above coloumn names should be changed to your database table's coloumn names.
c.Open();
cmd.ExecuteNonQuery();
Label1.Text = "Data Submited Successfully";
}
catch(Ex)
{
Console.WriteLine(Ex.Message);
}
finally
{
c.Close();
}
}
Note: Please be careful about data types of the coloumn while making the table. also coloumn names and table name should be matching to your database.
If all the things are followed correctly then you must be able to insert the data into database. Refresh your database and see in your database whether the data has been inserted correctly or not.
Please feel free to comment as it helps and motivates me to write the blog.
Thank you!!
-Vaibhav Patil.
No comments:
Post a Comment