Windows Develop Bookmark and Share   
 index > Windows Forms Data Controls and Databinding > Wrrrrr........ saving a "null value" when null NOT allowed ??????????????
 

Wrrrrr........ saving a "null value" when null NOT allowed ??????????????

I have a table in a SQL DB with a primary key where null is NOT allowed = no checkmark in the table definition.

Nevertheless I can save a row where the primary key is "not there" -it's a string of length 0 !

I just made my forms by Drag&Dropping a datasource to the form.

I have call-validation .

How can I prevent that a string of length 0 is inserted in my primary key column ?
PSAKSor  Wednesday, March 25, 2009 11:27 AM


If you want to show the error message, you can handle the CellValidating event.

Something like :

private void refer01DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

if (e.ColumnIndex == 0)

{

if (e.FormattedValue.ToString().Trim() == "")

{

MessageBox.Show("Not allow null");

e.Cancel = true;

}

}

}


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byPSAKSor Thursday, April 02, 2009 11:18 AM
  •  
Ling Wang  Wednesday, April 01, 2009 12:45 PM
Oh ... my God !

The bindingnavigator is the cause ... !

It's so easy to Drag&Drop a datasource to a form and get your data presented in a datagridview and it SEEMS like it's working ... but it's not !

One of my columns is the primary key in the DB and null values is NOT allowed in the DB in this field.

But you can add a new row in the datagridview, move away from the primary key column, enter some values in the other columns and now it comes .... click on the "move to previous" key and wupti the row is saved with a primary key as a string of length 0 !

How can this Drag&Drop method be made secure concerning data validation ?

PSAKSor  Wednesday, March 25, 2009 12:30 PM

Hi PSAKSor,

Could you please upload your project or a demo project and the database? That will help us make the issue more clearly.

Base on my understanding, there may be something wrong with the datatable(or dataset) in the project. Are you sure the null value can be saved which the corresponding column is not allowed null value? If so, I think you need to refresh the dataset in the databse and see if the corresponding column has a right setting. If not, I think there may be something wrong in the code.

Look forward to your reply.

Best regards,

Ling Wang


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Tuesday, March 31, 2009 11:44 AM
As I did write in my post "now it comes .... click on the "move to previous" key and wupti the row is saved with a primary key as a string of length 0 !"

It's a string of length 0 witch I think is the problem - it should taken care of.


PSAKSor  Wednesday, April 01, 2009 5:55 AM

If the primary key is identity, when saving the null value, the SQL Server will fill the blank with the corresponding value automatically.

Do you mean you problem has been solved? If not, please feel free to ask.


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
Ling Wang  Wednesday, April 01, 2009 6:12 AM
I don't think it's solved because I wants the user to see a message like "You should enter a primary key !" and then set focus back in the right column in the datagridview or textbox.

Where should I test for the "string of length 0" ?(with I believe is NOT a null string - right ?)

PSAKSor  Wednesday, April 01, 2009 8:03 AM


If you want to show the error message, you can handle the CellValidating event.

Something like :

private void refer01DataGridView_CellValidating(object sender, DataGridViewCellValidatingEventArgs e)

{

if (e.ColumnIndex == 0)

{

if (e.FormattedValue.ToString().Trim() == "")

{

MessageBox.Show("Not allow null");

e.Cancel = true;

}

}

}


Please remember to click “Mark as Answer�on the post that helps you, and to click “Unmark as Answer�if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread.
  • Marked As Answer byPSAKSor Thursday, April 02, 2009 11:18 AM
  •  
Ling Wang  Wednesday, April 01, 2009 12:45 PM

You can use google to search for other answers

Custom Search

More Threads

• how to reading inbox messages in C#
• how can i determine if the current record has been EDITED & changes have been saved to database ?
• Data Binding With radio Buttons
• Error found after i set MS access a password
• Getting Bindings
• Problem getting data from previous page submission
• Help needed dinding a datagrid to properies of a business entity (including relations)
• Dynamic Combobox in Datagrid
• datagrid view customizing by end user
• Data column format to currency