kdeutsch
asked on
Insert null into sql db via aspx page
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)
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.
ASKER
CodeCruiser:
Yes I allow nulls for that field in the Db, but on insert its still setting the default date in there.
Yes I allow nulls for that field in the Db, but on insert its still setting the default date in there.
Are you using the calendar control on page. It returns default dates.
ASKER
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"></a sp:TextBox >
<asp:CalendarExtender ID="txtCal_CalendarExtende r" runat="server" TargetControlID="txtCal" />
</ItemTemplate>
</asp:TemplateField>
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"></a
<asp:CalendarExtender ID="txtCal_CalendarExtende
</ItemTemplate>
</asp:TemplateField>
You would have to add a check so that if no date is selected, null is added to the field.
ASKER
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("t xtMData"), TextBox)
Dim Remarks As TextBox = TryCast(row.FindControl("t xtMRemarks "), TextBox)
Dim Dtdate As TextBox = TryCast(row.FindControl("t xtMCal"), TextBox)
Dim answer As RadioButtonList = TryCast(row.FindControl("r blMedical" ), 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
For Each row As GridViewRow In myGridMedical.Rows
Dim QuesId As Integer = myGridMedical.DataKeys(row
Dim data As TextBox = TryCast(row.FindControl("t
Dim Remarks As TextBox = TryCast(row.FindControl("t
Dim Dtdate As TextBox = TryCast(row.FindControl("t
Dim answer As RadioButtonList = TryCast(row.FindControl("r
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
If you step through the code, what happens on the following condition?
If Dtdate.Text = "" Then
If Dtdate.Text = "" Then
ASKER
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.
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.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
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.
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.
> then they have to convert everything.
But currently they have to test for nulls.
But currently they have to test for nulls.
ASKER
Changed to a string value in server, this allows what I need it to do with easier coding.