Click here to Skip to main content
15,909,373 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
do anybody have code for storing and displaying name and address field in which sql server is used as database and asp.net and c#
I have given the code below..I am getting confused about database code...and please explain all the steps..

XML
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>

<!DOCTYPE html>

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body style="height: 143px">
    <form id="form1" runat="server">
    <div>
    <asp:Label ID="label1" runat="server">Name :</asp:Label>
    &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="TextBox1" runat="server" Height="14px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

        <br />

        <br />
           <asp:Label ID="label2" runat="server">Address :</asp:Label>
    &nbsp;&nbsp;&nbsp;

        <asp:TextBox ID="TextBox2" runat="server" Height="14px" OnTextChanged="TextBox1_TextChanged"></asp:TextBox>

        <br />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
        <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Button" />
        <br />
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

    </div>
    </form>
</body>
</html>
Posted
Updated 7-Oct-13 0:16am
v2
Comments
jaideepsinh 7-Oct-13 3:46am    
Can you explain in detail what you want to do and what you tried?

Sorry I dont know c# but you can get the idea from this code.
I'll Assume you have textbox (txtFirst.txt txtlast.txt) controls on your apsx page and a
Button to submit and gridview or something to databind

Put this code inside your Button click event.
Replace with your own connection string from your web.config file
Replace strings with your own values
Replace Table1 with the name of your sql Table name


VB
Dim con As New SqlConnection
        Dim cmd As New SqlCommand
        Dim firstname, lastname  As String


        Try
            con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString
            con.Open()
           
            cmd.Connection = con
            cmd.CommandText = "INSERT INTO Table1([FirstName], [LastName]) VALUES('" & firstname & "','" & lastname & "')"
            cmd.ExecuteNonQuery()

        Catch ex As Exception
            Response.Write("Error while inserting record on table..." + ex.Message + "Insert Records")
            
        Finally
            con.Close()
        End Try
        DataBind()
 
Share this answer
 
v3
Here is C# that I got from language converter tool.

C#
SqlConnection con = new SqlConnection();
SqlCommand cmd = new SqlCommand();
string firstname = null;
string lastname = null;

try {
	con.ConnectionString = ConfigurationManager.ConnectionStrings("ConnectionString").ToString;
	con.Open();

	cmd.Connection = con;
	cmd.CommandText = "INSERT INTO Table1([FirstName], [LastName]) VALUES('" + firstname + "','" + lastname + "')";
	cmd.ExecuteNonQuery();

} catch (Exception ex) {
	Response.Write("Error while inserting record on table..." + ex.Message + "Insert Records");

} finally {
	con.Close();
}
DataBind();
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900