Thursday 13 February 2014

insert record in database(one tire)

--> your database and table like
    database name :student
    table name        :tbl_StudentData
    Field name         :id,name,age,city,phone


--> The following code copy paste in your .aspx page
------------------------------------------------------------------------------------------------



<body>
    <form id="form1" runat="server">
   
    Name
    <asp:TextBox ID="TextBox2" runat="server"></asp:TextBox><br/>
          
    Age
    <asp:TextBox ID="TextBox3" runat="server"></asp:TextBox><br/>
          
    Gender         
    <asp:RadioButtonList ID="RadioButtonList1" runat="server"
        RepeatDirection="Horizontal">
        <asp:ListItem>Male</asp:ListItem>
        <asp:ListItem>Female</asp:ListItem>
    </asp:RadioButtonList><br/>
          
    City          
    <asp:DropDownList ID="DropDownList1" runat="server">
        <asp:ListItem>- select city-</asp:ListItem>
        <asp:ListItem>Surat</asp:ListItem>
        <asp:ListItem>Bhavnagar</asp:ListItem>
        <asp:ListItem>Rajkot</asp:ListItem>
        <asp:ListItem>Ahmedabad</asp:ListItem>
        <asp:ListItem>Mumbai</asp:ListItem>
    </asp:DropDownList><br/>
          
    Phone
    <asp:TextBox ID="TextBox4" runat="server"></asp:TextBox><br/><br/>
          
  <asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Insert" />
    </form>
</body>

               


--> following code copy in your .aspx.cs file

--> if you want to add new record in table or database that time add following namespace in your code behind file
------------------------------------------------------------------------------------------------

   

using System.Data;
using System.Data.SqlClient;


-->and then create following  object above pag load event
------------------------------------------------------------------------------------------------

  

    SqlConnection cn;
    SqlCommand cmd;
    SqlDataAdapter da;  
    DataSet ds;


--> next assign the connection string in page load event
------------------------------------------------------------------------------------------------

 

cn = new SqlConnection(@"DataSource=\student.mdf;Integrated Security=True;User Instance=True");


--> and then write following code in your code behind on click event
------------------------------------------------------------------------------------------------


protected void Button1_Click(object sender, EventArgs e)
    {
       cmd = new SqlCommand("insert into tbl_StudentData values('" + TextBox2.Text + "'," + TextBox3.Text + ",'" + RadioButtonList1.SelectedItem + "','" + DropDownList1.SelectedItem + "','" + TextBox4.Text + "' )", cn);

        cn.Open();
        cmd.ExecuteNonQuery();
        cn.Close();
    }

No comments:

Post a Comment



Asp.net tutorials