0% found this document useful (0 votes)
21 views

Sidewinder2 Gold: Author: Member Level: Member Rank: 0 Date: 28/aug/2008 Rating:: 6

This document provides a code sample to store user input from a textbox into a database table when a save button is clicked. The code defines a button click event handler that builds an SQL insert statement to insert the textbox value into the specified table. It opens a database connection, executes the insert query, and closes the connection. The author recommends using a stored procedure instead and provides an example stored procedure for inserting employee name values along with a class to handle database operations.

Uploaded by

Victor Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Sidewinder2 Gold: Author: Member Level: Member Rank: 0 Date: 28/aug/2008 Rating:: 6

This document provides a code sample to store user input from a textbox into a database table when a save button is clicked. The code defines a button click event handler that builds an SQL insert statement to insert the textbox value into the specified table. It opens a database connection, executes the insert query, and closes the connection. The author recommends using a stored procedure instead and provides an example stored procedure for inserting employee name values along with a class to handle database operations.

Uploaded by

Victor Pathak
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Let user will enter a name in a textbox called txtUserName and while clicking save button following code

have
to use to store textbox value in database while button click event

protected void btnSave_Click(object sender, EventArgs e)


{
string lstrQuery = "INSERT INTO TABLE_NAME (USER_NAME) VALUES (' + txtUserName.Text.Trim() + ')";
System.Data.SqlClient.SqlConnection lobjConnection = new
System.Data.SqlClient.SqlConnection(CONNECTION_STRING);

System.Data.SqlClient.SqlCommand lobjCommand = new System.Data.SqlClient.SqlCommand(lstrQuery,


lobjConnection);
lobjConnection.Open();
lobjCommand.ExecuteNonQuery();
lobjConnection.Close();
}

This is sample to store data in textbox in database.

Author: Sidewinder2      Member Level: Gold      Member Rank: 0     Date:

28/Aug/2008   Rating:     Points: 6

hi ,

you can make a stored procedure for this,

example,

in the web config file

hi,
try this example.

first create a stored procedure like this

CREATE PROC INS_Employee


@EMPLOYEE_NAME VARCHAR(20) AS
BEGIN
INSERT INTO EMPLYEE_TABLE
(
EMPLOYEE_NAME
)
VALUES
(
@EMPLOYEE_NAME
)
END

connectionStrings>
<add name="SQL" connectionString="Server=Servername or ipaddress
;Database=databasename;uid=userid;pwd=password ;providerName="System.Data.SQLClient"/>
</connectionStrings>

then maks a seperate class for handing database operations and add it to your Appcode

like,

public class Database


{

string Conn_String;

public Database()
{
Conn_String = ConfigurationManager.ConnectionStrings["SQL"].ConnectionString;
}

public void ExecuteNonQuery(string procName, object[] parameters)


{
int ctr = 0;
ctr = SqlHelper.ExecuteNonQuery(Conn_String, procName, parameters);
}

then in the code behind,

protected void btnSave_Click(object sender, EventArgs e)


{

Database dbobj = newDatabase();


object[] Parameters = new object[1];

try
{

Parameters[0] = txtname.Text;

dbobj.ExecuteNonQuery("INS_Employee",Parameters);
}
catch (Exception ex)
{

Response.Write(ex.ToString());
}

You might also like