How to save data in database with only using C# coding?

Posted by Sf_Hussain786 under C# on 4/19/2012 | Points: 10 | Views : 34000 | Status : [Member] | Replies : 3
Hi All,

I want to ask you how to save data in database sql server using only c# code with using data table and connection string make in web.config file ?

Regarads,

sf_hussain786


Responses

Posted by: Dotnetrajanikanth on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Be more specific please unable to get you

____________
www.flickr.com/photos/psdesigner/

Sf_Hussain786, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: Sunny4989 on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
Good Question

Refer the below link

http://msdn.microsoft.com/en-us/library/xzb1zw3x.aspx

------------------------------------------------
Learn throughout life

Sf_Hussain786, if this helps please login to Mark As Answer. | Alert Moderator

Posted by: GSM_GSV on: 4/19/2012 [Member] Starter | Points: 25

Up
0
Down
C#

protected void BtnSubmit_Click(object sender, EventArgs e) {

// Get Connection String
ConnectionStringSettings conn = System.Configuration.ConfigurationManager.ConnectionStrings("YourDB");
// Create connection object
SqlConnection connection = new SqlConnection(conn.ToString);
SqlCommand command = connection.CreateCommand();
try {
// Open the connection.
connection.Open();
// Execute the insert command.
command.CommandText = ("INSERT INTO Your_Tbl(FirstName,LastName) VALUES(\'"
+ (this.TxtFirstName.Text + ("\',\'"
+ (this.TxtLastName.Text + ("\',\'"
))));
command.ExecuteNonQuery();
}
finally {
// Close the connection.
connection.Close();
}
// Display success message.
MsgBox("Insertion Successful");
}


Webconfig

<connectionStrings>

<add name="YourDB" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database1.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>


---------------------------------------
Live the life you've dreamed

Regards
MADHU

Sf_Hussain786, if this helps please login to Mark As Answer. | Alert Moderator

Login to post response