Jump to content

Help with C#

vern021

I need help in my C# project and it seems that all of my forms have a problem with the input to my SQL Database. I'm mostly stuck on my record creation and update in windows form.

 

here's the codes:

{
            SqlCommand cmdUpdate = new SqlCommand();
            cmdUpdate.Connection = cn;
            cmdUpdate.CommandType = CommandType.Text;
            cmdUpdate.Parameters.Add("@Lastname", SqlDbType.VarChar).Value = txtLastName.Text;
            cmdUpdate.Parameters.Add("@firstname_lastname", SqlDbType.VarChar).Value = txtFirstName.Text;
            cmdUpdate.Parameters.Add("@MI", SqlDbType.VarChar).Value = txtMI.Text;
            cmdUpdate.Parameters.Add("@Addressunknown", SqlDbType.VarChar).Value = txtAddress.Text;
            cmdUpdate.Parameters.Add("@TelNo", SqlDbType.VarChar).Value = txtTelNo.Text;
            cmdUpdate.Parameters.Add("@CustomerBday",SqlDbType.DateTime).Value = dtpBirthdate.Value;
            cmdUpdate.Parameters.Add("@CustomerStatus", SqlDbType.VarChar).Value = cboStatus.SelectedItem.ToString();
            cmdUpdate.Parameters.Add("@Registration",SqlDbType.DateTime).Value =dtpRegistration.Value;
            cmdUpdate.Parameters.Add("@CustomerNo", SqlDbType.Int).Value =int.Parse(txtCustNo.Text); //getting my error in this line
            cmdUpdate.CommandText = "UPDATE Customers2 " +
                " SET Lastname = @Lastname, " +
                " Firstname = @firstname_lastname, " + " MI = @MI, " +
                " Address = @Addressunknown, " + " TelNo = @TelNo, " +
                " CustomerBday = @CustomerBday, " + " CustomerStatus = @CustomerStatus, " +
                " Registration = @Registration, " +
                " CustPicture = @CustPicture " +
                " WHERE (CustomerNo = @CustomerNo)";
            Image bmp;
            System.IO.MemoryStream ms;
            if (picImage.Image== null)
            {
                bmp =Bitmap.FromFile(Application.StartupPath +@"\Anonymous.jpg");
                ms = new System.IO.MemoryStream();
                bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
                cmdUpdate.Parameters.Add("@CustPicture",SqlDbType.Image).Value =ms.GetBuffer();
            }
            else if (openFileDialog1.FileName != "")
            {
                bmp =Bitmap.FromFile(openFileDialog1.FileName);
                ms = new System.IO.MemoryStream();
                bmp.Save(ms,System.Drawing.Imaging.ImageFormat.Bmp);
                cmdUpdate.Parameters.Add("@CustPicture",SqlDbType.Image).Value =ms.GetBuffer();
            }
            else
            {
                cmdUpdate.CommandText= "UPDATE Customers2 " +
                    " SET Lastname= @Lastname, "+
                    "Firstname= @firstname_lastname, " +
                    " MI = @MI, " +
                    " Address = @Addressunknown, " +
                    "TelNo= @TelNo, " +
                    "CustomerBday= @CustomerBday, " +
                    "CustomerStatus= @CustomerStatus, " +
                    " Registration = @Registration " +
                    " WHERE CustomerNo= @CustomerNo";
            }
            cmdUpdate.ExecuteNonQuery();
        }
        //14
        private void DeleteRecord()
        {
            int nPosition = 0;
            if (cm.Count > 0)
            {
                if (MessageBox.Show("Are you sure?", "Confirmation",MessageBoxButtons.YesNo, MessageBoxIcon.Question) == DialogResult.Yes)
                {
                    nPosition = cm.Position;
                    SqlCommand cmdDelete = new SqlCommand();
                    cmdDelete.Connection = cn;
                    cmdDelete.CommandType = CommandType.Text;
                    cmdDelete.Parameters.Add("@CustomerNo",SqlDbType.Int).Value =txtCustNo.Text;
                    cmdDelete.CommandText= "DELETE Customers2 WHERE CustomerNo= @CustomerNo";
                    cmdDelete.ExecuteNonQuery();
                    RemoveDataBinding();
                    GetRecords();
                    AddDataBinding();
                    cm.Position = nPosition;
                    DisplayRecordPosition();
                    DisplayPicture();
                    if (cm.Count <= 0)
                    {
                        InitializeNewRecord();
                        txtCustNo.Text = "";
                        txtCustNo.Select();
                        cboStatus.SelectedIndex =-1;
                        cboStatus.Text = "";
                        lblRecordPosition.Text = "Empty";
                    }//end of cm.Count <= 0
                    MessageBox.Show("A record has just been deleted.", "Delete",MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
                else
                {
                    MessageBox.Show("You preferred not to delete this record.", "Delete Cancel", MessageBoxButtons.OK, MessageBoxIcon.Information);
                }
            }
//end of 1st IF
            else
            {
                MessageBox.Show("There is no record available as of this moment.","Empty Table", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }

 

I'm not sure if I have other errors since I'm really stuck with this one. Any help is highly appreciated.

My Current PC Codename: Scrapper

Spoiler

Intel i5-3570 | Some LGA 1155 MOBO Some Generic DDR3 8GB 1600Mhz | PowerColor RX 560 2GB | Recycled HP Case Crucial MX100 128GB 1TB WD Blue 7200RPM | Some Generic 500w PSU | Intel Stock Cooler

Link to comment
Share on other sites

Link to post
Share on other sites

what error is it saying exactly? you say you get an error at the line for setting the customer number value but what is the error given?

Link to comment
Share on other sites

Link to post
Share on other sites

what error is it saying exactly? you say you get an error at the line for setting the customer number value but what is the error given?

It occurs when I add a record using my form

lBhXNeX.png

My Current PC Codename: Scrapper

Spoiler

Intel i5-3570 | Some LGA 1155 MOBO Some Generic DDR3 8GB 1600Mhz | PowerColor RX 560 2GB | Recycled HP Case Crucial MX100 128GB 1TB WD Blue 7200RPM | Some Generic 500w PSU | Intel Stock Cooler

Link to comment
Share on other sites

Link to post
Share on other sites

It seems to me like txtCustNo.Text doesn't contain a valid integer. Maybe short.TryParse (If you want Int16) or int.TryParse (if you want Int32) should be used and you can handle the invalid value appropriately.

Link to comment
Share on other sites

Link to post
Share on other sites

It would be helpful to post what version of the .Net runtime you're using. For example, madknight3 has a good point about using the .TryParse functions, but if you're on .NET < 2.0 then those functions don't exist. If you're using int.Parse() for validation then you're doing that wrong; try using regular expressions as they work well for that task. Lastly, don't store a phone number as a numerical value. It's a string of numbers, like a U.S. zip-code. I'll give you an example of why you won't want to do this. Lets say you're trying to store the number for the US Embassy in China which is 010-8531-4000. An integer will not store the leading 0 so you'll be recording the wrong data.

Link to comment
Share on other sites

Link to post
Share on other sites

Dude... attach a file next time.dont post rough code

It's ok now don't worry xD

My Current PC Codename: Scrapper

Spoiler

Intel i5-3570 | Some LGA 1155 MOBO Some Generic DDR3 8GB 1600Mhz | PowerColor RX 560 2GB | Recycled HP Case Crucial MX100 128GB 1TB WD Blue 7200RPM | Some Generic 500w PSU | Intel Stock Cooler

Link to comment
Share on other sites

Link to post
Share on other sites

It's ok now don't worry xD

Could you post regarding how you fixed the problem and what was occuring? This may be helpful for someone who is experiencing a similar issue.

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×