Quantcast
Channel: Programming Forums
Viewing all articles
Browse latest Browse all 51036

Inserting NULL Values to Database C#

$
0
0
Okey This is my Table Structure

CREATE TABLE USERS
(
[User_ID] INT IDENTITY,
First_Name VARCHAR(20) NOT NULL,
Last_Name VARCHAR(20) NOT NULL,
BirthDay DATE,
Age INT,
Salary MONEY

CONSTRAINT USERS_PK PRIMARY KEY([User_ID])
)


As you can see Birth Day / Age / Salary Fields are optional

I use this line of code to handle this type of situation (Prevent From Format Exception)

string FirstName = FirstnameTextBox.Text; // Requred
            string LastName = LastNameTextBox.Text;  // Requred


            DateTime? BirthDay = null;
            int? Age = null;
            decimal? salary = null;




           

                // Handling Null Fields

                if (dateTimeInput1.Value.ToShortDateString()!= "1/1/0001")
                {

                    BirthDay = dateTimeInput1.Value;
                
                }

                if (!string.IsNullOrWhiteSpace(AgeTextBox.Text))
                {
                    Age = int.Parse(AgeTextBox.Text.ToString());
                }

                if (!string.IsNullOrWhiteSpace(AgeTextBox.Text))
                {

                    salary = decimal.Parse(SalaryTextBox.Text.ToString());
                }

       

            

            try
            {


                OFFICEDataSetTableAdapters.USERSTableAdapter UserAdapter_Connector = new OFFICEDataSetTableAdapters.USERSTableAdapter();

                UserAdapter_Connector.Insert(FirstName, LastName, BirthDay, Age, salary);

                MessageBox.Show("Record Inserted");
            }


         


            catch (Exception ex)
            {
                MessageBox.Show("Error has occured -->"+ex.Message.ToString());
            }

            


However This is working

Is there any easy method to archive this task ?

Viewing all articles
Browse latest Browse all 51036

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>