Please start any new threads on our new
site at https://forums.sqlteam.com. We've got lots of great SQL Server
experts to answer whatever question you can come up with.
Author |
Topic |
Shep
Starting Member
19 Posts |
Posted - 2013-06-13 : 14:09:51
|
I am completely new to this stuff.I need a step by step on how to enter data from a textbox to a database.Here is what I have so far...textboxs named: subDt, startDt, endDt, empId, reqTypeButtonServer name: developmentdb1Database: PTOtable to insert into: RequestFormsIf someone could tell me the actual code and where to put it would help alot. I would be able to evalute and understand from that.Thanks in advance for any help,JSJ.E.Shepler |
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-06-13 : 16:19:27
|
STEP1 : Extract the data from your text boxesRead this for more information: http://social.msdn.microsoft.com/Search/en-US/vstudio?query=TextBox&Refinement=195&ac=2STEP2: Open a connection to your databaseRead: http://msdn.microsoft.com/en-us/library/ms171886(v=vs.80).aspxSTEP3: Write queries to update your database tablehttp://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspxYou will find lot of example code here:http://code.msdn.microsoft.com/ |
|
|
Shep
Starting Member
19 Posts |
Posted - 2013-06-14 : 11:00:45
|
Thanks for the step by step. Here is what I have so far, but I'm still not sure how to "insert" it into the table. protected void Button1_Click1(object sender, EventArgs e) { int Submit; Submit = Convert.ToInt32(subDt.Text); int Start; Start = Convert.ToInt32(startDt.Text); int End; End = Convert.ToInt32(endDt.Text); int Employee; Employee = Convert.ToInt32(empId.Text); int Request; Request = Convert.ToInt32(reqType.Text);Also, the database connection has been made as well in the Server Explorer. It is listed in the Data Connections.Now to just write the insert statement. But I'm not sure how the information is actually SENT to the database. Do you have to make a connection when the button is clicked? This is the part I need the most help with. I understand the actually "INSERT" statement, just not the connection part. Can you please elaborate on that just a bit.Thanks again.JS |
|
|
MuMu88
Aged Yak Warrior
549 Posts |
Posted - 2013-06-14 : 12:47:51
|
Assuming you are using C#, you need connection string like this:[CODE]public String connectionString= "user id= username;" + "password=XXX;" + @"server=developmentdb1;" + "database=PTO; " + "connection timeout=30";[/CODE]If you already have something like that in your code use that string:Open connection and insert data as shown in the example below:[CODE]private static void UpdateDemographics(Int32 customerID, string demoXml, string connectionString){ // Update the demographics for a store, which is stored // in an xml column. string commandText = "UPDATE Sales.Store SET Demographics = @demographics " + "WHERE CustomerID = @ID;"; using (SqlConnection connection = new SqlConnection(connectionString)) { SqlCommand command = new SqlCommand(commandText, connection); command.Parameters.Add("@ID", SqlDbType.Int); command.Parameters["@ID"].Value = customerID; // Use AddWithValue to assign Demographics. // SQL Server will implicitly convert strings into XML. command.Parameters.AddWithValue("@demographics", demoXml); try { connection.Open(); Int32 rowsAffected = command.ExecuteNonQuery(); Console.WriteLine("RowsAffected: {0}", rowsAffected); } catch (Exception ex) { Console.WriteLine(ex.Message); } }}[/CODE] |
|
|
Shep
Starting Member
19 Posts |
Posted - 2013-06-14 : 15:26:23
|
Made some changes and came up with this: protected void Button1_Click1(object sender, EventArgs e) { string cnnString = WebConfigurationManager.ConnectionStrings["PTORequestDB"].ConnectionString; string sSQL = "INSERT INTO RequestForms (StartDate, EndDate, SubmitDate, EmployeeID, RequestTypeID) " + "VALUES(@StartDate, @EndDate,@SubmitDate, @EmployeeID, @RequestTypeID) SELECT SCOPE_IDENTITY()"; using (SqlConnection cnn = new SqlConnection(cnnString)) { cnn.Open(); SqlCommand cmd = new SqlCommand(sSQL, cnn); cmd.Parameters.AddWithValue("@StartDate", startDt.Text); cmd.Parameters.AddWithValue("@EndDate", endDt.Text); cmd.Parameters.AddWithValue("@SubmitDate", subDt.Text); cmd.Parameters.AddWithValue("@EmployeeID", empId.Text); cmd.Parameters.AddWithValue("@RequestTypeID", reqType.Text); cmd.ExecuteNonQuery(); cnn.Close();Thanks again for the pointersJS |
|
|
|
|
|
|
|