.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Forum >> C# >> Post New QuestionBookmark and Share Subscribe to Forum

Save name, Age through text box in database

Posted By: Manish     Posted Date: July 15, 2013    Points:5   Category :C#
Dear All,


i want to create a application which is stores name,address in sql database.

i have create a form in which i create 2 text box and a save button now what is the code i write down in this form.

i have create a service database and create the table which have name and address field. is it right or not?

pls give me the solution.

Regards
Manish Arya


Responses
Author: Krishna             
Posted Date: July 15, 2013     Points: 20   

Hi Manish,

On your web form place a button control,on button click event handler write code base for connecting your database and execute insert query command using System.Data class.

Please refer below code base for reference.

string Connection = "data source=your Server Details;initial catalog=service;integrated security=true";
SqlConnection _con = new SqlConnection(Connection);
SqlCommand cmd = new SqlCommand("Insert command", _con);
cmd.ExecuteNonQuery();


Author: satyapriyanayak             
Posted Date: July 15, 2013     Points: 20   


<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="WebForm1.aspx.cs" Inherits="Register.WebForm1" %>

<!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>Untitled Page</title>
</head>
<body>
<form id="form1" runat="server">
<div>

</div>
<asp:TextBox ID="txt_name" runat="server"></asp:TextBox>
<p>
<asp:TextBox ID="txt_age" runat="server"></asp:TextBox>
</p>
<asp:Button ID="Button1" runat="server" onclick="Button1_Click" Text="Button" />
</form>
</body>
</html>




using System;
using System.Collections;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
namespace Register
{
public partial class WebForm1 : System.Web.UI.Page
{
string strConnString = ConfigurationManager.ConnectionStrings["MyConsString"].ConnectionString;
string str;
SqlCommand cmd;


protected void Button1_Click(object sender, EventArgs e)
{
SqlConnection con = new SqlConnection(strConnString);
con.Open();
str="INSERT INTO test VALUES (@name, @age)";
cmd = new SqlCommand(str,con);
cmd.CommandType = CommandType.Text;
cmd.Parameters.AddWithValue("@name", txt_name.Text);
cmd.Parameters.AddWithValue("@age", txt_age.Text);
cmd.Connection = con;
cmd.ExecuteNonQuery();
con.Close();
Response.Write("Records Inserted");


}
}
}



If this post helps you mark it as answer
Thanks
Author: Manish             
Posted Date: July 17, 2013     Points: 20   

Sorry Sir,

This coding is not sufficiant for me. I want created a Windows Application Not a Web Application. which is i deploy in my system.When i open the software then enter a name and age value in the text box,now i click on the save button then it show a message "value save in the databse".and the click on the delete button,show a message "delete value in the database".

Now describe whole the program which is i created :-

1.Firstly i open a c# windows apllication form then creat a two textboxes and two button Save and Delete.

2.Now i am create a database, Right click on solution Explorer->Add->New Item-> Service Based select then cancel.

3.Now create a table, name "userid" in server exlporer.

4.Table have two field username and age now i fill the table (some database value like my name and age) through "show table".

5.Now i added a Reference SqlServerCe and add a namespace then create a Sql conncection.

6.If This coding and database creation is right then Please tell me other coding of that program Step by step.



form1.cs coding file:-

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlServerCe;


namespace WindowsFormsApplication4
{
public partial class Form1 : Form
{

SqlConnection con = new SqlConnection("Data Source=USER-PC;Initial Catalog=project1;Integrated Security=True");


public Form1()
{
InitializeComponent();
}

private void button1_Click(object sender, EventArgs e)
{

}
}
}





Post Reply

You must Sign In To post reply
 
 
Find more Forum Questions on C#, ASP.Net, Vb.Net, SQL Server and more Here
Quick Links For Forum Categories:
ASP.NetWindows Application  .NET Framework  C#  VB.Net  ADO.Net  
Sql Server  SharePoint  OOPs  SilverlightIISJQuery
JavaScript/VBScriptBiztalkWPFPatten/PracticesWCFOthers
www.DotNetSpark.comUnAnsweredAll

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend