Forum     

Go Back   Digit Technology Discussion Forum > Software > Programming
Register FAQ Calendar Mark Forums Read

Programming The destination for developers - C, C++, Java, Python and the lot


Closed Thread
 
LinkBack Thread Tools Display Modes
Old 12-03-2008, 12:58 PM   #1 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2007
Posts: 26
Exclamation inserting data from SQL Server using C# in ASP.NET


We have to insert data into Sql server from the form of Asp.net using C Sharp.can neone help me in this respect !!! I need the code basically..
garv84 is offline  
Advertisements. Register and be a member of the community to get rid of them.
Advertisement

Old 14-03-2008, 03:07 AM   #2 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: inserting data from SQL Server using C# in ASP.NET

Whats the problem dude? Forllow these steps :-

1. Create a web for with required fields as textboxes or whatever and a button to submit it.

For Example :

Code:
<%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!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>
    <asp:Panel runat ="server" ID="panel1">
    <asp:label ID="label1" runat="server" Font-Size="XX-Large">Data Entry Form</asp:label><br /><br />
        Sno : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
        Name : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
        Age : <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
        Salary<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /><br />
        <asp:Button ID="Button1" runat="server" Text="Insert Record" />
     </asp:Panel>
        </div>
    </form>
</body>
</html>
2. in the code behind, insert a using System.Data.SqlClient; statement.

Following is a generic code for inserting data:

Code:
protected void Button1_Click(object sender, EventArgs e)
    {
        SqlDataAdapter da = new SqlDataAdapter("Select * from Emp", "Server=sumitslaptop\\sqlexpress; database=Practice; integrated security=true;");
        SqlCommandBuilder cb = new SqlCommandBuilder(da);
        DataSet ds = new DataSet();
        da.FillSchema(ds, SchemaType.Source);
        DataRow dr = ds.Tables[0].NewRow();
        dr[0] = int.Parse(TextBox1.Text);
        dr[1] = TextBox2.Text;
        dr[2] = int.Parse(TextBox3.Text);
        dr[3] = double.Parse(TextBox4.Text);
        ds.Tables[0].Rows.Add(dr);
        try
        {
            da.Update(ds);
        }
        catch (Exception ex)
        {
            Response.Write(ex.Message);
            return;
        }
        Response.Write("Record Added.");
    }
Remember to change the connection string according to your computer. Also the code will vary according to the database schema. The given code is designed for following table :

Code:
create table Emp
(
Sno int,
Name varchar(50),
Age int,
Salary money
);
And do refer to your books dude, its very basic stuff. If you need one, i'll recommend
Quote:
Apress Pro ASP.NET 2.0 with C# 2005
for ASP.NET 2.0.
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Old 15-03-2008, 09:15 PM   #3 (permalink)
Right Off the Assembly Line
 
Join Date: Jul 2007
Posts: 26
Default Re: inserting data from SQL Server using C# in ASP.NET

thanx a lot bro..will definitely try it out..thanxx again..
garv84 is offline  
Old 16-03-2008, 02:45 AM   #4 (permalink)
Alpha Geek
 
Krazy_About_Technology's Avatar
 
Join Date: Jun 2004
Location: Noida - India
Posts: 765
Default Re: inserting data from SQL Server using C# in ASP.NET

No problemo
__________________
Dell Inspiron 1525 - C2D 2 Ghz, 3GB, 250GB, X3100 :)

Samsung Omnia Pro B7610 with Stock WM 6.1 ROM

Blog: http://www.sumitbhardwaj.co.in/blog
Krazy_About_Technology is offline  
Closed Thread

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
example of Inserting and Retrieving data from xml file hanusoft Software Q&A 1 26-09-2007 04:54 PM
Knowing Who changed data in MS SQL SERVEr rajesh_nk22 Software Q&A 1 30-06-2006 12:00 PM
Possible avoiding inserting CD evertime? a_medico QnA (read only) 3 04-10-2005 03:32 PM
Help me regarding inserting images to posts .. ayush456 QnA (read only) 3 22-12-2004 12:29 PM

 
Latest Threads
- by Charan
- by Sarath
- by clmlbx

Advertisement




All times are GMT +5.5. The time now is 12:54 AM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2012, vBulletin Solutions, Inc.

Search Engine Optimization by vBSEO 3.3.2