简体   繁体   中英

Inserting information from ASP.NET web page to Database

I am trying to allow the user to enter in employee information using textbooks and a drop down menu. For some reason that I cannot see at the moment, It is not inserting correctly into the database. I tried all the queries in the SqlCommandMode and they all worked correctly, but that was before I used the parameters from the function. Here is the code That I have in the DBconn.cs page

    public void insertEmployeeInfo(string firstName, string middleName, string lastName, string address1, string address2, string city, string postalCode, string state)
    {
        SqlConnection conn = new SqlConnection(strConnString);
        string sql1 = "INSERT INTO Person.BusinessEntity(ModifiedDate) VALUES (GETDATE())";
        string sql2 = "INSERT INTO Person.Person (ModifiedDate, BusinessEntityID, PersonType, Title, FirstName, MiddleName, LastName, Suffix) VALUES (GETDATE(), (SELECT MAX(PB.BusinessEntityID) AS IDNUM FROM PErson.BusinessEntity AS PB), 'EM', @title, @firstName, @lastName, @suffix)";
        string sql3 = "INSERT INTO Person.Address (AddressLine1, AddressLine2, City, StateProvinceID, PostalCode, ModifiedDate) VALUES (@address1, @address2, @city, @postalCode, (SELECT StateProvinceID FROM Person.StateProvince WHERE StateProvinceCode=@state))";

        try
        {
            conn.Open();
            SqlCommand cmd1 = new SqlCommand(sql1, conn);
            SqlCommand cmd2 = new SqlCommand(sql2, conn);
            SqlCommand cmd3 = new SqlCommand(sql3, conn);
            SqlParameter[] personParams = new SqlParameter[3];
            SqlParameter[] addressParams = new SqlParameter[5];

            //Params for inserts into Person.Person
            personParams[0] = new SqlParameter("@firstName", SqlDbType.VarChar, 50);
            personParams[1] = new SqlParameter("@middleName", SqlDbType.VarChar, 50);
            personParams[2] = new SqlParameter("@lastName", SqlDbType.VarChar, 50);

            //Params for inserts into Person.Address
            addressParams[0] = new SqlParameter("@address1", SqlDbType.VarChar, 50);
            addressParams[1] = new SqlParameter("@address2", SqlDbType.VarChar, 50);
            addressParams[2] = new SqlParameter("@city", SqlDbType.VarChar, 50);
            addressParams[3] = new SqlParameter("@postalCode", SqlDbType.VarChar, 50);
            addressParams[4] = new SqlParameter("@state", SqlDbType.VarChar, 50);

            personParams[0].Value = firstName;
            personParams[1].Value = middleName;
            personParams[2].Value = lastName;

            addressParams[0].Value = address1;
            addressParams[1].Value = address2;
            addressParams[2].Value = city;
            addressParams[3].Value = postalCode;
            addressParams[4].Value = state;

            //insert ModifiedDate into Person.BusinessEntity
            cmd1.CommandType = CommandType.Text;
            cmd1.ExecuteNonQuery();

            for (int i = 0; i < personParams.Length; i++)
            {
                cmd2.Parameters.Add(personParams[i]);
            }

            //insert new employee name information into Person.Person
            cmd2.CommandType = CommandType.Text;
            cmd2.ExecuteNonQuery();

            for (int i = 0; i < addressParams.Length; i++)
            {
                cmd3.Parameters.Add(addressParams[i]);
            }

            //insert new employee address information
            cmd3.CommandType = CommandType.Text;
            cmd3.ExecuteNonQuery();
        }
        catch (System.Data.SqlClient.SqlException ex)
        {
            string msg = "Insert Error:";
            msg += ex.Message;
            throw new Exception(msg);
        }
        finally
        {
            conn.Close();
        }
    }

And then here is what i am using to call the sql on the button click for the page.aspx.cs page

    protected void Insert_Click(object sender, EventArgs e)
{
    dbcon.insertEmployeeInfo(TxtFirstName.Text, TxtMiddleName.Text, TxtLastName.Text, TxtAddress1.Text, TxtAddress2.Text, TxtCity.Text, TxtPostalCode.Text, State.Text);
    Response.Write("Record was successfully added!");
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM