473,385 Members | 1,901 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How can I add data in Database using textbox in C#

Hi all,

I have tried to do a test to a lesson which was in the internet, but it doesn't work?

ANYBody here to help please??
The problem that what I enter in the textbox should be sent to the database to be stored, but it doesn't.!!! I don't know why



the aspx. page...

Expand|Select|Wrap|Line Numbers
  1.  
  2. <%@ Page Language="C#" AutoEventWireup="true"  CodeFile="Default.aspx.cs" Inherits="_Default" %>
  3.  
  4. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  5.  
  6. <html xmlns="http://www.w3.org/1999/xhtml">
  7. <head id="Head1" runat="server">
  8.     <title>Untitled Page</title>
  9. </head>
  10. <body>
  11.     <form id="form1" runat="server">
  12.     <div>
  13.     <asp:Panel runat ="server" ID="panel1">
  14.     <asp:label ID="label1" runat="server" Font-Size="XX-Large">Data Entry Form</asp:label><br /><br />
  15.         Sno : <asp:TextBox ID="TextBox1" runat="server"></asp:TextBox><br /><br />
  16.         Name : <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br /><br />
  17.         Age : <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br /><br />
  18.         Salary<asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br /><br />
  19.         <asp:Button ID="Button1" runat="server" Text="Insert Record" />
  20.      </asp:Panel>
  21.         </div>
  22.     </form>
  23. </body>
  24. </html>
  25.  
  26.  
  27.  


the aspx.cs file code


Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.Data;
  3. using System.Configuration;
  4. using System.Collections;
  5. using System.Web;
  6. using System.Web.Security;
  7. using System.Web.UI;
  8. using System.Web.UI.WebControls;
  9. using System.Web.UI.WebControls.WebParts;
  10. using System.Web.UI.HtmlControls;
  11.  
  12. public partial class Default4 : System.Web.UI.Page
  13. {
  14.     protected void Page_Load(object sender, EventArgs e)
  15.     {
  16.         SqlDataAdapter da = new SqlDataAdapter("Select * from Emp", "Server=localhost; database=CV; integrated security=true;");
  17.         SqlCommandBuilder cb = new SqlCommandBuilder(da);
  18.         DataSet ds = new DataSet();
  19.         da.FillSchema(ds, SchemaType.Source);
  20.         DataRow dr = ds.Tables[0].NewRow();
  21.         dr[0] = int.Parse(TextBox1.Text);
  22.         dr[1] = TextBox2.Text;
  23.         dr[2] = int.Parse(TextBox3.Text);
  24.         dr[3] = double.Parse(TextBox4.Text);
  25.         ds.Tables[0].Rows.Add(dr);
  26.         try
  27.         {
  28.             da.Update(ds);
  29.         }
  30.         catch (Exception ex)
  31.         {
  32.             Response.Write(ex.Message);
  33.             return;
  34.         }
  35.         Response.Write("Record Added.");
  36.     }
  37.  
  38. }
  39.  
  40.  
  41.  

and I have a database with

"Emp" as table name
"Sno" as int type
"Name" as varchar(50) type
"Age" as int type
"Salary" as money type
Oct 27 '08 #1
4 9740
kenobewan
4,871 Expert 4TB
Its difficult to know where to start. There is no should, this cannot work.

My suggestion is find a good tutorial, try the how to's and the sticky at the top of the forum. We assume in this forum that you know the basics.

My first hint is that there is no button click event.
Oct 27 '08 #2
Its difficult to know where to start. There is no should, this cannot work.

My suggestion is find a good tutorial, try the how to's and the sticky at the top of the forum. We assume in this forum that you know the basics.

My first hint is that there is no button click event.

I discovered this problem ealier, could you please help me on it??

or could you find with me a good tutorial??
Iam so tired of searching...!!!
Oct 27 '08 #3
Curtis Rutland
3,256 Expert 2GB
Find yourself a good book. I like the Wrox books.

Make sure that it is a book/tutorial on basic ASP.NET w/ C#.

Without having thoroughly read your code, like Ken said, you need to add a button click event handler. You really need to research that one on your own, because it is very basic, and very fundamental to learn ASP.NET. You get a better understanding for these kinds of things when you've had to work for it.
Oct 27 '08 #4
kenobewan
4,871 Expert 4TB
Microsoft has an MDSN library that is a good reference point:
Button..::.Click Event

Here is one that will give you a better idea of dynamic events:
Dynamic Events on an ASP.NET Page

Also try looking at the Howtos section of this site :).
Oct 28 '08 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: scurenton | last post by:
First off - New to Programming. Problem: I created an aspx page with x amount of text boxes. These text boxes can be filled by the user and when they click the submit button I want the data in...
2
by: TN Bella | last post by:
PLEASE HELP!! I am so far behind....and it doesn't help that I am learning all this stuff. I am using .net framework 1.0 from WebMatrix. These parameters get me all confused...someone please help!...
2
by: naija naija | last post by:
Hello guys i made a Datagrid with Editing,Update and Cancel using VS.NET. to my surprise nothing is on the screen after compilation .. By code below:- Imports System.Data Imports...
2
by: Robert Smith jr. | last post by:
Hello, Please pardon my newbie question ... I am building an ASP.NET page that displays a recordset with a Delete statement enabled (this all works fine). I want to Insert the current row...
10
by: Doug Bell | last post by:
Hi, I have an application that has a "Data Access Class" and "User Interface Class". It is for receiving Purchase Order data from one system and pushing processed transactions to another...
5
by: hfk0 | last post by:
Hi, I'm new to ASP.net, SQL Server and visual studio.net, and I'm having problem inserting and storing data from a web form to a SQL database. I created a simple ASP.NET web form, a simple SQL...
0
by: pd123 | last post by:
I'm new to C# and .net and I'm trying to create a form that will register users in a sql server database. I have the following code but when I run the code I get an error " The name 'Peter' is...
1
by: thithi | last post by:
Please help me " System.Data.SqlClient.SqlException: Prepared statement '(@Name varchar(100),@Describes varchar(100),@Money char(10))Upda' expects parameter @Describes, which was not supplied. at...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.