简体   繁体   中英

C# WinForm List into Database

Afternoon All,

I have been tasked with turning what is currently a paper-based form which our users fill in, into an electronic form in a Windows Form c# application, where the user can fill it in electronically then click a button, which puts the data in the database.

I have already completed 5 other forms with no issues, however the one I have just reached, which I thought would be the simplest, has stumped me.

This is an example of what the paper one looks like and how it is filled in (it gets printed from excel first):

纸质表格形式

My database has the following tables:

User
   UserID
   UserName

EquipmentReturnSubmission
   UserID (from User table)
   ReturnID
   ReturnDate

 EquipmentReturnDetails
   ReturnID (from EquipmentReturnSubmission table)
   SerialNo
   Description

When the data is put into the database, each row on the form above will have a row in the EquipmentReturnDetails table, but all have the same ReturnID, so this can be linked to produce a list of the equipment submitted by that user.

The bit that has stumped me is how to do this in my WinForms application. I've had a look at inserting data from a GridView into a database, but can only find how to do this one row at a time - i need this to insert all of the rows using the same ReturnID so it can be linked.

I thought I could do something like below, but not a clue where to start to get it coded, nor even if this is the best way to do it.

尝试形式

My thinking is that the user enters the serial number and description, and clicks add, which puts the details into listbox/gridview or some kind of holding area, and clears the text boxes. The user can then keep doing this, each time the details are added to the holding area, then the submit button writes it to the database.

Again i'm not sure how this could be done unless there's a way to create a parameter each time the Add button is clicked.

If anyone could point me in the right direction that would be great. I'm self taught so happy to be completely corrected.

Thanks in advance.

Well, thanks to Zath's comment I did a bit more research and managed to get this working with the below:

Tables:

User
    UserID
    UserName

 EquipmentReturnSubmission
    UserID //(from User table)
    ReturnID
    ReturnDate

EquipmentReturnDetails
    ReturnID //(from EquipmentReturnSubmission table)
    SerialNo
    Description

I then added a DataGridView to my form, and added the columns SerialNo and Description. My C# code below:

 private void Submit_Click(object sender, EventArgs e)
    {
        if (MessageBox.Show("Are you sure these details are correct?", "Are you sure?", MessageBoxButtons.YesNo) == DialogResult.Yes)
        {
                dataGridView1.AllowUserToAddRows = false; //Disables edit so the insert doesnt try and do an insert for the blank row created when the user clicks to add data.

                #region SQL Insert

                SqlConnection con = new SqlConnection(Home.ConString);
                SqlCommand cmd = new SqlCommand("EquipmentReturnSubmission1", con);
                cmd.CommandType = CommandType.StoredProcedure;
                #region Parameters

                cmd.Parameters.Add("@Name", SqlDbType.VarChar, 200).Value = OfficerName.Text;
                cmd.Parameters.Add("@Area", SqlDbType.VarChar, 200).Value = Area.Text;
                cmd.Parameters.Add("@SubmissionDate", SqlDbType.Date).Value = SubmissionDate.Value;
                cmd.Parameters.Add("@SubmissionID", SqlDbType.Int).Direction = ParameterDirection.Output;

                #endregion
                try
                {
                    con.Open();
                    cmd.ExecuteNonQuery();
                }
                catch (System.Exception ex1)
                {
                    throw new System.Exception("Error submitting equipment return sheet." + ex1.Message);
                }
                finally
                {
                    SubID = int.Parse(Convert.ToString(cmd.Parameters["@SubmissionID"].Value));
                    con.Close();
                    SecondInsert();
                }
                #endregion
            }
        }
    }


    private void SecondInsert()
    {
        foreach (DataGridViewRow row in dataGridView1.Rows)
        {

            SqlConnection con = new SqlConnection(Home.ConString);
            {
                SqlCommand cmd = new SqlCommand("INSERT INTO EquipmentReturnSubmissionDetails (SubmissionID, SerialNumber, Description) VALUES (@SubmissionID, @SerialNumber, @Description)", con);
                cmd.CommandType = CommandType.Text;
                {
                    cmd.Parameters.AddWithValue("@SubmissionID", SqlDbType.Int).Value = SubID;
                    cmd.Parameters.AddWithValue("@SerialNumber", row.Cells["SerialNo"].Value);
                    cmd.Parameters.AddWithValue("@Description", row.Cells["Description"].Value);
                    con.Open();
                    cmd.ExecuteNonQuery();
                    con.Close();
                }
            }

        }
        MessageBox.Show("Data submitted.");
        this.Close();
    }

This worked exactly as I required it to and does it very quickly.

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