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());
}