Avatar of Zbiebu
Zbiebu

asked on 

Application allowing null values in DB table

Hi,

I have an application  that requires some fields to mandatory aand I have created the DB tables not to allow nulls. When  runing an insert from the Management Studio, the insert fails if the mandatory fields are null.
When I try the same insert using the application, the process allows nulls to be entered into required fields.
Any help would be appreciated.
Thanks
Microsoft SQL ServerVisual Basic.NET

Avatar of undefined
Last Comment
Zbiebu
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

That can not happen because database engine will through the exception if you try to insert null into non null field. What is happening probably is that you are inserting empty strings into those fields. When you view the table in management studio, you would see the field as empty as opposed to containing null. Show us the insert code.
Avatar of Zbiebu
Zbiebu

ASKER

 Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        'Declare local variables and objects
        Dim objCommand As SqlCommand = New SqlCommand()

        'Set the Command object properties
        Dim objConnection As New SqlConnection _
            ("server=Server1;database=brc;integrated Security=true")
        Dim IDParm As New SqlParameter("@IDenty", SqlDbType.Int)
        IDParm.Direction = ParameterDirection.Output
        'Set the SqlCommand object properties

        'Open the connection and execute the command

        objCommand.Connection = objConnection
        objConnection.Open()
        objCommand.CommandText = "usp_InsertBRCDetails"
        objCommand.CommandType = CommandType.StoredProcedure

        'Add the parameters for the placeholders in the SQL CommandText Property

        'Parameter for the Title Column
        objCommand.Parameters.AddWithValue("@Title", txtTitle.Text)

        'Parameter for the Description Column
        objCommand.Parameters.AddWithValue("@Description", txtDesc.Text)

        'Parameter for the Section Column
        objCommand.Parameters.AddWithValue("@Section", txtSection.Text)

        'Parameter for the CompiledBy Column
        objCommand.Parameters.AddWithValue("@CompiledBy", txtCompiledBy.Text)

        'Parameter for the Link column
        objCommand.Parameters.AddWithValue("@Link", txtLink.Text)

        'Parameter for the Version column
        objCommand.Parameters.AddWithValue("@Version", txtVersion.Text)

        'Parameter for the Date Column
        objCommand.Parameters.AddWithValue("@Date", DTP1.Text)

        objCommand.Parameters.Add(IDParm)

        'Execute the SqlCommand object to insert the new data
        Try
            objCommand.ExecuteNonQuery()
        Catch SqlExceptionErr As SqlException
            MessageBox.Show(SqlExceptionErr.Message)

        End Try
        Dim NextID As Integer = Convert.ToInt32(IDParm.Value)
        MessageBox.Show(NextID, "The Document ID Is")
        'Close connection
        objConnection.Close()

        'Display a message that the record was added
        ToolStripStatusLabel1.Text = "Record Added"
        'Clear all fields
        txtTitle.Text = String.Empty
        txtDesc.Text = String.Empty
        txtSection.Text = String.Empty
        txtCompiledBy.Text = String.Empty
        txtLink.Text = String.Empty
        DTP1.Text = String.Empty

    End Sub

Open in new window

Hi,
Thanks for your reply. Here is the VB code for insert. When I checked the tables, the fields where empty as you say.
Thanks
When Textbox is empty the value of Text is empty string and it is not null.
so you are inserting emtpy strings to database.

if you want to pass null instead of empty string you can check Textbox's text value by string.IsNullOrEmpty(TextBox.Text) if it returns true, pass DBNull.Value instead of TextBox.Text.
Avatar of Zbiebu
Zbiebu

ASKER

Hi,

Would I be able to use this in the existing code or would I have to recode from scratch?
Thanks
SOLUTION
Avatar of Habib Pourfard
Habib Pourfard
Flag of New Zealand image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
See Pricing Options
Start Free Trial
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

In your stored procedure, put checks on parameters like this example

If(Len(@Title) = 0)
BEGIN
   SET @Title = Null
END

and so on for each parameter that has to be non null
ASKER CERTIFIED SOLUTION
Avatar of Nasir Razzaq
Nasir Razzaq
Flag of United Kingdom of Great Britain and Northern Ireland image

Blurred text
THIS SOLUTION IS ONLY AVAILABLE TO MEMBERS.
View this solution by signing up for a free trial.
Members can start a 7-Day free trial and enjoy unlimited access to the platform.
Avatar of Zbiebu
Zbiebu

ASKER

Many thanks to all
Microsoft SQL Server
Microsoft SQL Server

Microsoft SQL Server is a suite of relational database management system (RDBMS) products providing multi-user database access functionality.SQL Server is available in multiple versions, typically identified by release year, and versions are subdivided into editions to distinguish between product functionality. Component services include integration (SSIS), reporting (SSRS), analysis (SSAS), data quality, master data, T-SQL and performance tuning.

171K
Questions
--
Followers
--
Top Experts
Get a personalized solution from industry experts
Ask the experts
Read over 600 more reviews

TRUSTED BY

IBM logoIntel logoMicrosoft logoUbisoft logoSAP logo
Qualcomm logoCitrix Systems logoWorkday logoErnst & Young logo
High performer badgeUsers love us badge
LinkedIn logoFacebook logoX logoInstagram logoTikTok logoYouTube logo