Solved

Insert null into sql db via aspx page

Posted on 2011-02-11
12
221 Views
Last Modified: 2012-05-11
I am trying to substitue null values into the db for Date fields becuase I don't like how sql injects 19000101 automatically if nothing is there to insert.  how can I accomplish this and for that matter aother fields as well.


Dim Dtdate As TextBox = TryCast(row.FindControl("txtMCal"), TextBox)

  If Dtdate.Text = "" Then
                Dtdate.Text.
            End If

            sql = "Insert tblSRpEventData (intPersonnelId, intUnitSRPId, intQuestionId, bitAnswer, strData, strRemarks, dtDoc, dtLogged, strlogged) VALUES (" & PersId & ", " & UnitID & ", " & QuesId & ", " _
                    & "" & answer.SelectedValue & ", '" & data.Text & "', '" & Remarks.Text & "', '" & Dtdate.Text & "', '" & Date.Today & "', '" & userlogon & "')"

            insertUpdateDelete(sql)

Open in new window

0
Comment
Question by:kdeutsch
  • 6
  • 6
12 Comments
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34871793
Does the field allow nulls? Sql wont add the default date if null is allowed. The default date is set when null is read back into your program.
0
 

Author Comment

by:kdeutsch
ID: 34872126
CodeCruiser:
Yes I allow nulls for that field in the Db, but on insert its still setting the default date in there.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34872423
Are you using the calendar control on page. It returns default dates.
0
 

Author Comment

by:kdeutsch
ID: 34872536
Hi,
Yes I am using the calendar control inside a gridview as such.
<asp:TemplateField HeaderText="Record Date">
                                                            <ItemTemplate>
                                                                <asp:TextBox ID="txtCal" runat="server" Width="100" style="cursor:pointer"></asp:TextBox>
                                                                <asp:CalendarExtender ID="txtCal_CalendarExtender" runat="server" TargetControlID="txtCal" />                                                            
                                                            </ItemTemplate>
                                                          </asp:TemplateField>
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34872575
You would have to add a check so that if no date is selected, null is added to the field.
0
 

Author Comment

by:kdeutsch
ID: 34873479
What i am doing is rotating through my gridview when i save, because its an options field in the gridview, they don't have to put a date in there.  I tried the following inside of the ratating but a textbox does not have a value.  This would be the step to check it and from what I have seen on google where everyone checks it but my syntax fdoes not give me the value it blue underlines it.

 For Each row As GridViewRow In myGridMedical.Rows
            Dim QuesId As Integer = myGridMedical.DataKeys(row.RowIndex).Value
            Dim data As TextBox = TryCast(row.FindControl("txtMData"), TextBox)
            Dim Remarks As TextBox = TryCast(row.FindControl("txtMRemarks"), TextBox)
            Dim Dtdate As TextBox = TryCast(row.FindControl("txtMCal"), TextBox)
            Dim answer As RadioButtonList = TryCast(row.FindControl("rblMedical"), RadioButtonList)

            If Dtdate.Text = "" Then
                Dtdate.value = IsDBNull()
            End If

            sql = "Insert tblSRpEventData (intPersonnelId, intUnitSRPId, intQuestionId, bitAnswer, strData, strRemarks, dtDoc, dtLogged, strlogged) VALUES (" & PersId & ", " & UnitID & ", " & QuesId & ", " _
                    & "" & answer.SelectedValue & ", '" & data.Text & "', '" & Remarks.Text & "', '" & Dtdate.Text & "', '" & Date.Today & "', '" & userlogon & "')"

            insertUpdateDelete(sql)
        Next
0
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34883574
If you step through the code, what happens on the following condition?

  If Dtdate.Text = "" Then
0
 

Author Comment

by:kdeutsch
ID: 34916909
Hi,

Sorry just getting back to this, nothing happens on that point, it still goes through and puts in defaut date.  Would it be easier if I changed the datatime to a varchar, then I could insert nothing.
0
 
LVL 83

Accepted Solution

by:
CodeCruiser earned 350 total points
ID: 34917002
I use varchar to store dates.
0
 

Author Comment

by:kdeutsch
ID: 34917512
CodeCruiser,

I think i am going to back to that method because its to hard to try to insert null dates into a sql db, i was told not to do this, becuase then they have to convert everything.
0
 
LVL 83

Expert Comment

by:CodeCruiser
ID: 34917572
> then they have to convert everything.
But currently they have to test for nulls.
0
 

Author Closing Comment

by:kdeutsch
ID: 34917894
Changed to a string value in server, this allows what I need it to do with easier coding.
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
Converting C# Objects into JSON file 7 37
System32Int Error 8 44
Clone Visual studio 2013 solution 5 16
Getfiles in vb.net 20 0
User art_snob (http://www.experts-exchange.com/M_6114203.html) encountered strange behavior of Android Web browser on his Mobile Web site. It took a while to find the true cause. It happens so, that the Android Web browser (at least up to OS ver. 2.…
Calculating holidays and working days is a function that is often needed yet it is not one found within the Framework. This article presents one approach to building a working-day calculator for use in .NET.
Internet Business Fax to Email Made Easy - With eFax Corporate (http://www.enterprise.efax.com), you'll receive a dedicated online fax number, which is used the same way as a typical analog fax number. You'll receive secure faxes in your email, fr…
When you create an app prototype with Adobe XD, you can insert system screens -- sharing or Control Center, for example -- with just a few clicks. This video shows you how. You can take the full course on Experts Exchange at http://bit.ly/XDcourse.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

18 Experts available now in Live!

Get 1:1 Help Now