Click here to Skip to main content
16,017,167 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
i Have PRODUCT info text boxes which gets its values from a listview working
with a product ID now i want it to save that info to my MSSQL DB
i have a textbox for the first "Start" Serial and another textbox to put in a set amount
i want so if i add 0001 into serial textbox and 20 in the other it must Add from 0001-0020 into my SQL DB when i save With The same Product Info

What I have tried:

if (txtProductID.Text == "" || txtPSerial.Text == "" || txtCat.Text == "" || txtBin.Text == "-" || txtManufac.Text == "" || txtLocation.Text == "")
{
MessageBox.Show("Fill textboxes to proceed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
return;
}
else
{
try
{
string sql = @"INSERT INTO tblSerials VALUES(@PID,@Descrip,@Serials,@Catagory,@Location,@Bin,@Manufacturer,@Date,@DateOUT,@DateIN,@Employee,@INOUT)";
//
cm = new SqlCommand(sql, cn);
cm.Parameters.AddWithValue("@PID", txtPID3.Text);
cm.Parameters.AddWithValue("@Descrip", txtProductID.Text);
cm.Parameters.AddWithValue("@Serials", txtPSerial.Text);
cm.Parameters.AddWithValue("@Catagory", txtCat.Text);
cm.Parameters.AddWithValue("@Location", txtLocation.Text);
cm.Parameters.AddWithValue("@Bin", txtBin.Text);
cm.Parameters.AddWithValue("@Manufacturer", txtManufac.Text);
cm.Parameters.AddWithValue("@Date", lblDate.Text);
cm.Parameters.AddWithValue("@DateOUT", txt1.Text);
cm.Parameters.AddWithValue("@DateIN", txt2.Text);
cm.Parameters.AddWithValue("@Employee", txt3.Text);
cm.Parameters.AddWithValue("@INOUT", txt4.Text);

cm.ExecuteNonQuery();
MessageBox.Show("Record successfully saved!", "OK!", MessageBoxButtons.OK, MessageBoxIcon.Information);
//InsertTrail();
this.Clear();
this.GetSData();
txtPSerial.Text = "";



}
catch (SqlException l)
{
MessageBox.Show("Re-input again. ID may already be taken!");
MessageBox.Show(l.Message);
}

}
}
Posted
Updated 1-Oct-18 1:06am
Comments
OriginalGriff 1-Oct-18 1:46am    
And?
What is your problem?
Where are you stuck?
What help do you need?
Why the heck do you think this is a good idea in a multiuser DB?
Member 13894223 1-Oct-18 3:03am    
I can add single entries to data base i want it to add from txtboxAmount.text (the amount of serials i want say 20 it must add 20 entries into DB with same product info but with the serials from 0001 ,0002,0003 to 0020
OriginalGriff 1-Oct-18 3:26am    
If you know how to add one, why can't you extend that to add more?
What help do you need from us?

And again: "Why the heck do you think this is a good idea in a multiuser DB?"
Member 13894223 1-Oct-18 3:44am    
Im totally new to coding im learning as i go along but this the part im stuck on and not sure how to go about it

Quote:
Im totally new to coding im learning as i go along but this the part im stuck on and not sure how to go about it

But, you know how to do one, and you know - I assume - what a loop is.
You know how to get the content from a TextBox, and you know - I assume - how to convert that to an integer.

Combining those things is not a complex process!

But ... this is a bad idea, because SQL Server is a multiuser environment, which means that "preallocation" of data is a bad idea. Why? Because if two users decide to allocate the same or overlapping ranges everything gets messed up together and you get major problems - like the same serial number being issued to two objects.
Now, you may say "that won't happen" - but you have no idea what the code will be used for next week, next moth, next year - and if you do get multiple users entering data (and you will), then at some point it will happen - and then it becomes a real PITA to sort out, because the database no longer reflects reality and you have no idea what is going on. And these things are only ever noticed when it's too late to stop things and apply a "quick fix" ensuring your data integrity.


Look at what you are doing, and think about usage: it's almost certain that you need either a much better scheme than this, or you need a much better way to sort out which numbers have been allocated that a simple "block of twenty" approach.
 
Share this answer
 
if (txtProductID.Text == "" || txtAmount.Text == "" || txtPSerial.Text == "" || txtCat.Text == "" || txtBin.Text == "-" || txtManufac.Text == "" || txtLocation.Text == "")
            {
                MessageBox.Show("Fill textboxes to proceed.", "Message", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            else
            {
                try
                {
                    long serial = Convert.ToInt64(txtSerialStart.Text);
                    int qty = Convert.ToInt32(txtAmount.Text);
                    for(int i = 0;i < qty;i++)
                    {
                        serial = serial + i;
                        String serialX = serial.ToString();

                        string sql = @"INSERT INTO tblSerials VALUES(@PID,@Descrip,@Serials,@Catagory,@Location,@Bin,@Manufacturer,@Date)";
                        cm = new SqlCommand(sql, cn);
                        cm.Parameters.AddWithValue("@PID", txtPID3.Text);
                        cm.Parameters.AddWithValue("@Descrip", txtProductID.Text);
                        cm.Parameters.AddWithValue("@Serials", serialX);
                        cm.Parameters.AddWithValue("@Catagory", txtCat.Text);
                        cm.Parameters.AddWithValue("@Location", txtLocation.Text);
                        cm.Parameters.AddWithValue("@Bin", txtBin.Text);
                        cm.Parameters.AddWithValue("@Manufacturer", txtManufac.Text);
                        cm.Parameters.AddWithValue("@Date", lblDate.Text);

                        cm.ExecuteNonQuery();
                    }

                    MessageBox.Show("Record successfully saved!", "OK!", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    //InsertTrail();
                    this.Clear();
                    this.GetSData();
                    txtPSerial.Text = "";



                }
                catch (SqlException l)
                {
                    MessageBox.Show("Re-input again. ID may already be taken!");
                    MessageBox.Show(l.Message);
                }
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900