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

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • MissElegant
    New Member
    • May 2008
    • 17

    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...

    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 id="Head1" 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>


    the aspx.cs file code


    Code:
    using System;
    using System.Data;
    using System.Configuration;
    using System.Collections;
    using System.Web;
    using System.Web.Security;
    using System.Web.UI;
    using System.Web.UI.WebControls;
    using System.Web.UI.WebControls.WebParts;
    using System.Web.UI.HtmlControls;
    
    public partial class Default4 : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            SqlDataAdapter da = new SqlDataAdapter("Select * from Emp", "Server=localhost; database=CV; 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.");
        }
        
    }

    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
  • kenobewan
    Recognized Expert Specialist
    • Dec 2006
    • 4871

    #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.

    Comment

    • MissElegant
      New Member
      • May 2008
      • 17

      #3
      Originally posted by kenobewan
      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...!!!

      Comment

      • Curtis Rutland
        Recognized Expert Specialist
        • Apr 2008
        • 3264

        #4
        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.

        Comment

        • kenobewan
          Recognized Expert Specialist
          • Dec 2006
          • 4871

          #5
          Microsoft has an MDSN library that is a good reference point:
          Button..::.Clic k 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 :).

          Comment

          Working...