简体   繁体   中英

Insert into database using textbox control asp.net

I'm trying to insert data from control textbox into a database table; however say the user selects that they want to textboxes; then one ID in the textbox is entered in one row and the other ID is entered in another row in the database and I want it to entered as ID1 and ID2. I have 3 columns that user can enter in the IDs; so for instance they select 1 textbox then the data in that one textbox is entered and other columns remain null.

Update

using (var command = new SqlCommand("PP_CreateIDNumber", connection))
                    {
                        command.CommandType = CommandType.StoredProcedure;
                        command.Parameters.AddWithValue("@usernum", lblSheet.Text);

                        int counter = 1;
                        foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
                        {
                                string seal = string.Format("@seal{0}", counter++);
                                command.Parameters.AddWithValue(seal, textBox.Text);

                                command.ExecuteNonQuery();
                         }

}

foreach (TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>())
{
    using (SqlCommand comm = new SqlCommand("PP_CreateIDNumber", connection))
    {                  
            comm.CommandType = CommandType.StoredProcedure;

            comm.Parameters.AddWithValue("@userNum", lblUser.Text);
            comm.Parameters.AddWithValue("@ID1", textBox.Text);
            comm.Parameters.AddWithValue("@ID2", textBox.Text);
            comm.Parameters.AddWithValue("@ID3", textBox.Text);

            comm.ExecuteNonQuery();
            comm.Parameters.Clear();
        }
    }    
}

Stored Procedure

@usernum varchar(20),
    @ID1 nchar(10),
    @ID2 nchar(10),
    @ID3 nchar(10),



AS
BEGIN
    -- SET NOCOUNT ON added to prevent extra result sets from
    -- interfering with SELECT statements.
    SET NOCOUNT ON;

    -- Insert statements for procedure here
    insert into Student values(@usernum, @ID1,@ID2, @ID3)

在此处输入图片说明

If I understand correctly, then I believe you want something like this:

    using (var command = new SqlCommand("PP_CreateIDNumber", connection)) {
        command.CommandType = CommandType.StoredProcedure;
        command.Parameters.AddWithValue("@userNum", lblUser.Text);

        int counter = 1;
        foreach(TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>()) {
            command.Parameters.AddWithValue($"@ID{counter++}", textBox.Text);
        }

        command.ExecuteNonQuery();
    }

If you do not have access to a framework version supporting string interpolation in the manner I have used it above, you can use string.Format("@ID{0}", counter++) and that should be sufficient for any framework version.

You should additionally consider the order in which you should iterate over your textboxes and whether you have a need to impose a specific order. You should also consider validating that the number of textboxes does not exceed the number of foreign key columns to which you will be inserting data. Lastly, you should consider whether the design of this table is actually optimal. Since you are holding many foreign keys here, you should consider having a table designed for a one-to-many relationship.

Not having intimate knowledge of your schema makes it difficult to provide a specific suggestion here, but consider a table that has a UserNum column and simply an Id column (potentially even an additional column that indicates the sequence/order of the identities). Instead of using one row to hold the data, use many rows. I do not know your requirements and again your schema, so I feel sheepish about pushing you to a potentially unnecessary redesign of a table, but you should at least consider whether it is applicable here.

Lastly, consider not performing data access within your user interface. Have your user interface reference a separate assembly that holds classes that understand how to provide your UI with the data it requires.

All of that being said, I believe the code example gets you where you want to go at the moment.

Edit after substantial questions and conversation:

You need to supply the additional parameters for the number of textboxes that have not been chosen.

int maxPossibleTextBoxCount = 3;
int selectedTextBoxCount = ContentPlaceHolder1.Controls.OfType<TextBox>().Count();
int emptyTextBoxCount = maxPossibleTextBoxCount - selectedTextBoxCount;

using (var command = new SqlCommand("PP_CreateIDNumber", connection)) {
    command.CommandType = CommandType.StoredProcedure;
    command.Parameters.AddWithValue("@userNum", lblUser.Text);

    int counter = 1;

    // Here we add the parameters for the selected textboxes.
    foreach(TextBox textBox in ContentPlaceHolder1.Controls.OfType<TextBox>()) {
        command.Parameters.AddWithValue($"@ID{counter++}", textBox.Text);
    }

    // Here we add the parameters for the non-selected textboxes.
    if(emptyTextBoxCount > 0) {
        for(int i = 0; i < emptyTextBoxCount - 1; i++) {
            command.Parameters.AddWithValue($"@ID{counter++}", System.DBNull.Value);
        }
    }

    command.ExecuteNonQuery();
}

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