Thursday 13 February 2014

update record in database(one tire)

--> edit record in (database) Asp.net

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

--> Your .aspx page
------------------------------------------------------------------------------------------------

<div>
    Id
    <asp:textbox id="TextBox1" runat="server">
    </asp:textbox><br />
    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="Update" />
</div>
       
--> Copy following code in your .aspx.cs file

--> if you want to update 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 page 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
------------------------------------------------------------------------------------------------

        cmd = new SqlCommand("update tbl_StudentData set name='" + TextBox2.Text + "',age='" + TextBox3.Text + "',gender='" + RadioButtonList1.SelectedItem + "',city='" + DropDownList1.SelectedItem + "',phone='" + TextBox4.Text + "' where id='"+TextBox1.Text+"' ", cn);

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

No comments:

Post a Comment



Asp.net tutorials