Hello guys , i have created a form which has some texboxes in , where users will be inputing data in. now i want this data to be saved to a database . how can i do this connection ? can the database be created in visual stodio ?
thanks and regards to all.
Loading
azzo stonesPosted Sep 11, 2011, 2:00 PM
Jiteendra SampathiraoPosted Aug 25, 2011, 12:05 AM
1. create connectionstring
2. declare sqlconnection and sqlcommand object
3.open the connection string
4. write the insert query and store it as a string
5. use connection object and string value in sqlcommand
6. perform executenonquery
7. dispose the command object
8. close the connection string
hope it help you.........
azzo stonesPosted Aug 24, 2011, 10:22 AM
Satyapriya NayakPosted Aug 24, 2011, 7:56 AM
Here is your desired code.Run the attachments.
First create a test.mdf file (website-add new item -sql server database)
web.config setting
program
using System;
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;
public partial class _Default : System.Web.UI.Page
{ string connStr = ConfigurationManager.ConnectionStrings["ConnectionString"].ConnectionString;
SqlCommand com ;
SqlConnection con;
string str;
protected void Button1_Click(object sender, EventArgs e)
{
con = new SqlConnection(connStr);
con.Open();
str = "insert into student values('" + TextBox1.Text + "','" + TextBox2.Text + "','" + TextBox3.Text + "'," + TextBox4.Text + ",'" + TextBox5.Text + "')";
com = new SqlCommand(str, con);
com.ExecuteNonQuery();
con.Close();
Response.Redirect("sucess.aspx");
}
}
Thanks
If this post helps you mark it as answer
Jiteendra SampathiraoPosted Aug 24, 2011, 7:33 AM
In the web.config file under app settings
<appSettings>
<add key="Sql_server" value="Data Source=XXXXX;User ID=XXXXXX; password=XXXXX; Database=XXXXX">add>
appSettings>
In the button click event do this...
protected void Button1_click(object sender, EventArgs e)
{
ConnectionString();
}
public void ConnectionString()
{
string Emp_FName = TextBox4.Text;
string Emp_LName = TextBox5.Text;
string Emp_Gender = TextBox6.Text;
string Emp_P_City = TextBox7.Text;
string Emp_P_Country = TextBox8.Text;
string Emp_P_PIN = TextBox9.Text;
string Emp_Phone = TextBox10.Text;
string Emp_Bank_Name = TextBox11.Text;
string PAN_NO = TextBox12.Text;
Sqlconnection myconn = new Sqlconnection(ConfigurationManager.AppSettings["Sql_server"]);
myconn.Open();
Sqlcommand mycomm = new Sqlcommand();
mycomm.Connection = myconn;
mycomm.CommandType = CommandType.Text;
string myqry = "insert into test.emp_master(Emp_FName, Emp_LName, Emp_Gender, Emp_P_City, Emp_P_Country, Emp_P_PIN, Emp_Phone, Emp_Bank_Name, PAN_NO)
values
('" + Emp_FName + "','" + Emp_LName + "','" + Emp_Gender + "','" + Emp_P_City + "','" + Emp_P_Country + "','" + Emp_P_PIN + "','" + Emp_Phone + "','" + Emp_Bank_Name + "','" + PAN_NO + "')";
mycomm.CommandText = myqry;
SqlDataAdapter myadapter = new SqlDataAdapter(mycomm);
DataSet ds = new DataSet();
myadapter.Fill(ds);
mycomm.Dispose();
myconn.Close();
}
And check the data table now. you can see the record...
hope it help you.......