SQL Server Forums
Profile | Register | Active Topics | Members | Search | Forum FAQ
 
Register Now and get your question answered!
Username:
Password:
Save Password
Forgot your Password?

 All Forums
 General SQL Server Forums
 New to SQL Server Programming
 Insert from VS2012 to SQL db
 New Topic  Reply to Topic
 Printer Friendly
Author Previous Topic Topic Next Topic  

Shep
Starting Member

USA
19 Posts

Posted - 06/13/2013 :  14:09:51  Show Profile  Reply with Quote
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, reqType
Button
Server name: developmentdb1
Database: PTO
table to insert into: RequestForms

If 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,
JS

J.E.Shepler

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/13/2013 :  16:19:27  Show Profile  Reply with Quote

STEP1 : Extract the data from your text boxes
Read this for more information: http://social.msdn.microsoft.com/Search/en-US/vstudio?query=TextBox&Refinement=195&ac=2

STEP2: Open a connection to your database
Read: http://msdn.microsoft.com/en-us/library/ms171886(v=vs.80).aspx

STEP3: Write queries to update your database table
http://msdn.microsoft.com/en-us/library/aa260662(v=sql.80).aspx

You will find lot of example code here:
http://code.msdn.microsoft.com/
Go to Top of Page

Shep
Starting Member

USA
19 Posts

Posted - 06/14/2013 :  11:00:45  Show Profile  Reply with Quote
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
Go to Top of Page

MuMu88
Aged Yak Warrior

547 Posts

Posted - 06/14/2013 :  12:47:51  Show Profile  Reply with Quote
Assuming you are using C#, you need connection string like this:

public String connectionString= "user id= username;" + 
                                      "password=XXX;" +
                                    @"server=developmentdb1;" +
                                    "database=PTO; " +
                                    "connection timeout=30";


If you already have something like that in your code use that string:



Open connection and insert data as shown in the example below:


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);
        }
    }
}
Go to Top of Page

Shep
Starting Member

USA
19 Posts

Posted - 06/14/2013 :  15:26:23  Show Profile  Reply with Quote
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 pointers
JS
Go to Top of Page
  Previous Topic Topic Next Topic  
 New Topic  Reply to Topic
 Printer Friendly
Jump To:
SQL Server Forums © 2000-2009 SQLTeam Publishing, LLC Go To Top Of Page
This page was generated in 0.05 seconds. Powered By: Snitz Forums 2000