Click here to Skip to main content
15,923,789 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
i have database by access and i make my code for add data for employee the problem
i have data but not complete for each employee so i want take textbox empty

i mean

i want add data like ID and Date for start work and Date for Stop work
so i have now ID and i have Start work but not have stop work because he is not stop

when i leave textbox for stop work empty will give me error
because type of data in database Date so not allow leave it empty

when i let type for stop work in database with string
this give me allow leave textbox with empty when i insert data

but

when i load data
i can't order data because the type is string not date

can anyone help me please

What I have tried:

i have 3 column (ID, start work, stop work)
can't leave stop work textbox empty because type is date in database
any solution can let textbox with empty?
Posted
Updated 10-Dec-21 21:42pm

In your Access database right-click on your table and select "Design View"
Click on the stop work (please tell me that is not really the name of your column - get rid of the space)
At the bottom on the screen you should see the properties for that column.
In the General tab, make sure the value for "Required" is set to "No"

EDIT after OP Comment.

Your code is vulnerable to SQL Injection - never use string concatenation to build SQL strings especially if based on user input. Use Parameterised Queries. See SQL Injection | OWASP[^] and SQL Injection Prevention - OWASP Cheat Sheet Series[^]

The reason you are getting errors is because you are always trying to insert the StopWork column. Just omit that column if the text box is blank e.g. (warning code is untested)
C#
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;

// These parameters always required
cmd.Parameters.AddWithValue("InvoiceNo", txtInvoiceNo.Text);
cmd.Parameters.AddWithValue("ID", txtID.Text);
cmd.Parameters.AddWithValue("ShortName", txtShortName.Text);
cmd.Parameters.AddWithValue("EnterDate", txtEnterToSaudi.Text);
cmd.Parameters.AddWithValue("StartWork", txtStartWork.Text);

string ssql; 
if(txtStopWork.Text.Length > 0)
{
	ssql = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StartWork,StopWork) values (?,?,?,?,?,?)";
	// this parameter only required if StopWork textbox is not empty
	cmd.Parameters.AddWithValue("StopWork", txtStopWork.Text);
}
else
{
	// note there is one fewer ? marks
	ssql = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StartWork) values (?,?,?,?,?)";
}

cmd.CommandText = ssql;

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Saved Done for " + txtID.Text);
 
Share this answer
 
v2
Comments
ZaYeD1 8-Dec-21 12:39pm    
"Required" is set to "No"

it's No
all columns "Required" is set to "No"

i can't leave empty when i insert data (because date type) even when i make type of data number i can't leave textbox empty

only if make it (string type) that let me free and easy to deal with textbox
but can't use order in data grid view

note: the problem only when i try insert data by c#
if i try insert data inside access no problem

when i try change to SQL database it's same problem
the different when i leave textbox empty SQL give me default value 01/01/1900

i want it full empty
ZaYeD1 8-Dec-21 12:41pm    
my code
OleDbCommand cmd = new OleDbCommand();
cmd.Connection = connection;

cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StartWork,StopWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtEnterToSaudi.Text + "','" + txtStartWorkDate.Text + "','" + txtStopWork.Text + "')";

connection.Open();
cmd.ExecuteNonQuery();
connection.Close();
MessageBox.Show("Saved Done for " + txtID.Text);



my code is ok but i got the problem when i try save empty by C# in date type in access database
CHill60 8-Dec-21 13:28pm    
I have updated my solution with a fix to your problem
ZaYeD1 9-Dec-21 5:49am    
not work

EnterDate,StartWork,StopWork
it's same date type in database
and i can write full data and leave it empty by access
but with c#
i can write full data but i can't leave any (EnterDate,StartWork,StopWork)
because date type when change to string inside access database will work without any problem
but
order can't arrange column because it read data as string not as date

sorry for write more and sorry for bother you
-_-
CHill60 9-Dec-21 6:20am    
There is no need to change the type to string. Just don't insert the StopWork date if it is empty. When you enter the data in Access you are not explicitly telling the row to insert data into that column. Which is what you are doing with your code.
"Not work" is not helpful - exactly what happens when you use my code?
C#
<pre>
                OleDbCommand cmd = new OleDbCommand();
                cmd.Connection = connection;


                //txtEnterToSaudi.Text == ""
                if (txtEnterToSaudi.Text == "" && txtStartWorkDate.Text != "" && txtStopWork.Text != "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,StartWork,StopWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtStartWorkDate.Text + "','" + txtStopWork.Text + "')";
                }
                //txtStartWorkDate.Text == ""
                else if (txtEnterToSaudi.Text != "" && txtStartWorkDate.Text == "" && txtStopWork.Text != "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StopWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtEnterToSaudi.Text + "','" + txtStopWork.Text + "')";
                }
                //txtStopWork.Text == ""
                else if (txtEnterToSaudi.Text != "" && txtStartWorkDate.Text != "" && txtStopWork.Text == "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StartWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtEnterToSaudi.Text + "','" + txtStartWorkDate.Text + "')";
                }
                //txtEnterToSaudi.Text == "" && txtStartWorkDate.Text == ""
                else if (txtEnterToSaudi.Text == "" && txtStartWorkDate.Text == "" && txtStopWork.Text != "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,StopWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtStopWork.Text + "')";
                }
                //txtStartWorkDate.Text == "" && txtStopWork.Text == ""
                else if (txtEnterToSaudi.Text != "" && txtStartWorkDate.Text == "" && txtStopWork.Text == "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtEnterToSaudi.Text + "')";
                }
                //txtEnterToSaudi.Text == "" && txtStopWork.Text == ""
                else if (txtEnterToSaudi.Text == "" && txtStartWorkDate.Text != "" && txtStopWork.Text == "")
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,StartWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtStartWorkDate.Text + "')";
                }
                //txtEnterToSaudi.Text == "" && txtStartWorkDate.Text == "" && txtStopWork.Text == ""
                else if (txtEnterToSaudi.Text == "" && txtStartWorkDate.Text == "" && txtStopWork.Text == "")
                {
                    MessageBox.Show("please fill all data before press add");
                }
                //txtEnterToSaudi.Text != "" && txtStartWorkDate.Text != "" && txtStopWork.Text != ""
                else
                {
                    cmd.CommandText = "insert into Start_Stop_Work (InvoiceNo,ID,ShortName,EnterDate,StartWork,StopWork) values ('" + txtInvoiceNo.Text + "','" + txtID.Text + "','" + txtShortName.Text + "','" + txtEnterToSaudi.Text + "','" + txtStartWorkDate.Text + "','" + txtStopWork.Text + "')";
                }


                connection.Open();
                cmd.ExecuteNonQuery();
                connection.Close();
                MessageBox.Show("Saved Done for " + txtID.Text);
 
Share this answer
 
v2
Comments
CHill60 13-Dec-21 3:37am    
Your code is now very vulnerable to SQL injection attack. Use parameterised queries not this mess.

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