By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
435,573 Members | 3,017 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 435,573 IT Pros & Developers. It's quick & easy.

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

P: 17
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
Share this Question
Share on Google+
4 Replies


kenobewan
Expert 2.5K+
P: 4,871
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

P: 17
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
Expert 2.5K+
P: 3,256
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
Expert 2.5K+
P: 4,871
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

Post your reply

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