Options rather than O or null in database - c#

In my program if i doesn't select a drop down list they are storing as 0 or null values in table but they are not visible in view.Can any of u suggest me other option rather than 0 or null in database.

you can enter some default values in your specific column that can be done in several ways
on insert statement if dropdown value is not selected you can pass some default values like dash (-)
for specific table where you need default value you can set default value using query
ex:
ALTER TABLE UserDetails ADD CONSTRAINT DF_SomeName DEFAULT N'-' FOR Email_ID;
hope this will help

Related

Visual studio SQL default value not showing on windows form when adding new record

I have created a SQL table in Visual studio and set a default value on a field.
The table is connected to my C# project (in the same solution) and pulls through fine, however the default values are not appearing when a new row is created via a windows form.
Info from comment questions:
It doesn't appear on the form when I click the + for a new row.
When a row is added from the database the defaults are there.
Added a new row via the form, left the fields with default values blank, saved, re opened and no default values.
Based on this W3Schools Tutorials
The default value will be added to all new records IF no other value is specified.
So the Default value will not show until you save the row to the database, you can add it programatically, but the Default value constraint is used to assign a value if there is no values provided in this column
You can save these default values in the application Settings and use it when new row is added
textBox1.Text = Properties.Settings.Default.TextBoxDefaultValue;
If you are facing an issue when defining default value in Visual Studio try adding a Default Constraint using SQL query:
ALTER TABLE XXX
ADD CONSTRAINT def_Retired
DEFAULT 'N' FOR Retired;
UPDATE 1
On the form designer, click on the BindingNavigator, In the Properties window set the AddNewItem property to (none)
Try adding the following code to the bindingNavigatorAddNewItem_Click event
private void bindingNavigatorAddNewItem_Click(object sender, EventArgs e) {
vehiclesBindingSource.AddNew();
Textbox1.Text = Properties.Settings.Default.Text1default;
};
It doesn't appear on the form when I click the + for a new row.
The DataGridView does not know anything about default values in the database table. Accordingly, you must specify the default values in the DataGridView.
Or if you are using data binding, you can specify default values in the DataTable.
When a row is added from the database the defaults are there.
It's right, because in the database they are set.
Added a new row via the form, left the fields with default values blank, saved, re opened and no default values.
If you insert data into the database in the following way:
insert into SomeTable ('Retired') values ('');
then no default value will be used. With high probability I can assume that at the moment you passes empty values from DataGridView, if they are not set. This is wrong!
If you insert data into the database in the following way:
insert into SomeTable ('Last Mileage') values (0);
then the default value for Retired column will be used. It should be so.
Therefore, you must correctly design the Insert statement, completely omitting those columns for which should be used the default value in the database itself.
Summing up. You either have to fill the default values in the DataGridView/DataTable or dynamically construct the Insert statement, completely omitting the not specified values.
You are assigning default values in forms that are dynamically generated. You may need to evaluate whether the components of your client side/server side scripting were also made to accommodate this dynamic behavior in parallel.
Default Constraint Works only if you are not specifying the Column in the Insert List.
Suppose I have a Table Like This
CREATE TABLE MyInfo
(
Id INT IDENTITY(1,1),
Nm VARCHAR(50),
MyDate DATE DEFAULT(GETDATE())
)
I'm specifying the Values Like this
INSERT INTO MyInfo
(
Nm,
MyDate
)
SELECT
Nm = 'A',
MyDate = '02/23/93'
UNION
SELECT
Nm = 'B',
MyDate = NULL
And The Result will be Like this
Now I insert a row without specifying MyDate Column
INSERT INTO MyInfo
(
Nm
)
VALUES('C')
And the MyDate was populated bu Default values
Default Constraint is meant to assign the value if the column is not specified in the insert list. If what you need is to set a Default value if The Inserted value IS NULL or empty, Try using triggers

How to pull the data from a table with columns which allow nulls to the table of not null-able columns

i need to pull the data from table which has all columns of allowing null type to another table which have columns matching with previous table but of not null type.
So i need a query for pulling entire data of all the matching columns.with a default value in the position of null row.
I am assuming that a given column is of integer type and default value is 0. You can use below based on your RDBMS:
SQL Server
ISNULL(COL_1,0)
Oracle
NVL(COL_1,0)
or
COALESCE(COL_1,0)
MYSQL
IFNULL(COL_1,0)
or
COALESCE(COL_1,0)
Replace the col_1 with your column name and default value 0 with appropriate value

Bind default value if excel sheet column value null in sql bulk copy upload

I created mvc 4 application that can upload data into database using sql bulk copy upload method.
Once I select excel file, it can upload data into the system.
this is working fine, now I want to add default value if excel field is null
this is how it extract column values of excels
"s1.Gender, " +
"s1.Country_of_Birth, " +
"s1.Date_of_Birth, " +
I want to add default value from code level , I already handle this using database level.
for example to add default value for "date of birth" I added following constraint
ALTER TABLE [dbo].[tbl_HEI_student] ADD CONSTRAINT [DF_tbl_HEI_student_Date_of_Birth] DEFAULT (getdate()) FOR [Date_of_Birth]
But when I upload excel file into db ,SQL bulk copy upload method is ignoring adding that default value database .
How can I add default value from controller method
Your problem must be that the column allows NULLS.
In case your column allows NULL and you provide a NULL value on the insert the NULL WILL be inserted. If no value is provided for the column the DEFAULT will be considered.
Setup your column to NOT NULL.
ALTER TABLE [dbo].[tbl_HEI_student] ALTER COLUMN [Date_of_Birth] DATE NOT NULL
Here is the Default Definition.
EDIT:
As you have noticed, you can't BulkInsert the NULLS to that columns and make the DEFAULT value prevail on SqlServer side, and the NOT NULL i suggested will enforce that either no value is supplied or something other than NULL.
I can see a few options.
Either deal with them .NET side by setting the value to your default (you will have to show you .NET code if you want help with that)
doing one bulk for rows with value and another for rows without and removing the mapping for that column, the default constraint will take action
Setting the row back to allow NULLS, do the bulk as you were doing, and doing an update after the bulk to set the NULLS to your default value.
I know BULK INSERT and BCP both have a qualifier as shown here to deal with the NULL values. The same option is available in the .NET SqlBulkCopy options.
Here is another question related to your problem.
Keep in mind your database schema should help you enforce your business rules and keep your data clean. If the date should never be NULL, keep the column as NOT NULL and do your solution accordingly.

Cannot insert the value "Null" into column 'xyz'

I'm having an issue with adding new patients to my SQL database. When I do so, the following error appears, and an SQL Exception is thrown.
Cannot insert the value NULL into column 'ID', table 'IntelliMedDB.dbo.PatientRecord'; column does not allow nulls. INSERT fails.
The information contained in all of the values that are added as parameters aren't actually null. They all have information inside them.
Now, to protect against SQL Injection attacks, all of my queries use parameterized values.
I had an MS Access DB that handled all of this fine (same queries, I've just switched from using OleDBCommand to SqlCommand. Other than that, nothing's changed).
Here's the "INSERT INTO" statement, followed by a parameterized query, just so you all can see what I've done:
INSERT INTO PatientRecord (patientID, firstName, lastName, patientGender, dateOfBirth, residentialAddress, postalAddress, nationalHealthNumber, telephoneNumber, cellphoneNumber)
VALUES (#patientIDNumber, #recepPatFirstName, #recepPatLastName, #recepPatGender, #recepPatDateOfBirth, #recepPatResidentialAddress, #recepPatPostalAddress, #recepNHINumber, #recepPatTelephone, #recepPatCellularNumber)";
(I apologize for the length of the query).
And now, one of the parameterized values:
recepPatRecordCommand.Parameters.AddWithValue("#recepPatFirstName", patRecepFirstName);
Any help gratefully received.
Thank you!
You should edit your question with the definition of the table and the database you are using.
However, the problem is pretty clear. The PatientRecord table has a column called id which is declared to be NOT NULL. That means that NULL values are not allowed -- and you get the error that you see. By not setting a value explicitly in the insert, the value is set to a default. With no default, the value is set to NULL.
Normally, such columns have a default value or are declared to be identity (or auto incrementing).
I think you should probably fix the table so the NOT NULL columns have default values. Or, put in an appropriate value for the id column. For this, you would add id to the column list and then a corresponding value in the values list.
Although your INSERT statement refers to patientId, the error message you are seeing refers to ID.
You are seeing this error because no value for ID is specified.
Open your table in Design
Select your column and go to Column Properties
Under Indentity Specification, set (Is Identity)=Yes and Indentity Increment=1

Dynamic Update stored procedure SQL Server

I have a table with 30+ fields. Many of the fields allows NULL. My question is if I want to update the field(s) that changed value, how do I do that with stored procedure? Currently I am selecting a specific row and storing all the current values in the table. Next I would have to compare each variable to the new one to see if they are different, and if it is, then add that field name and the new variable to the string query. Then execute. Is there a more efficient way of doing this? As you imagine, it is a pain to compare all 30+ fields, especially since many are Nullable. Thank you.
So here is my logic so far in my stored procedure:
Have all the parameters that are nullable and set them to null (since it is optional).
DECLARE a variable for each field which I will be testing (30+ fields)
SELECT the row identify by the ID passed in from the parameter (primary key).
Store all the values into each variable.
Check each value to see if it IS NULL. If so, that means the user did not provide the field so no update in necessary for that field. If the parameter value IS NOT NULL, check if it is == to the value SELECTED from the database. If the value !=, then add it to the query string to be updated.
Execute the update query string.

Categories

Resources