Thursday, 2 January 2014

Store data using asp.net / Insert data into database using asp.net

Posted on 05:29 in
Introduction:
This article is helps to how to insert data into database in asp.net using Vb.net
Here textbox, Dropdown list and radiobuttonlist are Using store Record in the database.
Designing Page(Source Page) :
<!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></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:Label ID="lblName" runat="server" Text="Name"></asp:Label>
<asp:TextBox ID="txtName" runat="server"></asp:TextBox>
<br />
<asp:Label ID="lblDepartment" runat="server" Text="Department"></asp:Label>
<asp:DropDownList ID="ddlDepartment" runat="server">
<asp:ListItem Text="CSE" Value="CSE"></asp:ListItem>
<asp:ListItem Text="ECE" Value="ECE"></asp:ListItem>
<asp:ListItem Text="EEE" Value="EEE"></asp:ListItem>
<asp:ListItem Text="MECH" Value="MECH"></asp:ListItem>
</asp:DropDownList>
<br />
<asp:Label ID="lblGender" runat="server" Text="Gender"></asp:Label>
<asp:RadioButtonList ID="rbtnGender" runat="server">
<asp:ListItem Text="Male" Value="Male" Selected="True"></asp:ListItem>
<asp:ListItem Text="Female" Value="Female"></asp:ListItem>
</asp:RadioButtonList>
<br />
<asp:Button ID="btnSave" runat="server" Text="Save" />
</div>
</form>
</body>
</html>

VB.NET:
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            txtName.Text = String.Empty
            ddlDepartment.SelectedIndex = 0
            rbtnGender.SelectedValue = "Male"
        End If
End Sub

Protected Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim con As New SqlConnection("server=192.168.100;uid=sa;pwd=sa123;database=asp_dotnet")
        con.Open()
        Dim cmd As New SqlCommand("INSERT INTO studentdetails(name,gender,department) Values('" + txtName.Text.ToString() + "','" + rbtnGender.SelectedValue.ToString() + "','" + ddlDepartment.SelectedValue.ToString() + "')", con)
        cmd.ExecuteNonQuery()
        con.Close()
        MsgBox("saved successfully"
End Sub
C#.NET:
protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtName.Text = string.Empty;
                ddlDepartment.SelectedIndex = 0;
                rbtnGender.SelectedValue = "Male";
            }
        }
protected void btnSave_Click(object sender, EventArgs e)
        {
            SqlConnection con = new SqlConnection("server=192.168.1.100;uid=sa;pwd=sa123;database=asp_dotnet");
            con.Open();
            SqlCommand cmd = new SqlCommand("INSERT INTO studentdetails(name,gender,department) Values('" + txtName.Text.ToString() + "','" + rbtnGender.SelectedValue.ToString() + "','" + ddlDepartment.SelectedValue.ToString() + "')", con);
            cmd.ExecuteNonQuery();
            con.Close();
            ClientScript.RegisterOnSubmitStatement(this.GetType(),"ConfirmSubmit", "alert('saved successfully')");
        }
Demo:





0 comments:

Post a Comment