[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 809
  • Last Modified:

inserting data into sql database from modalpopup

I am trying to insert data into a sql database using the ajax.modalpopupextender control.  I don't get an error of any kind, but the data just will not show up in the database.   I have already verified the connection string as being correct.  And the code works in SqlManagementStudio.  I guess it has something to do with the Modalpopupextender.

Can anyone help?  


Thanks

This is the code behind for the DieHistoryDone_click event.


Dim ChangeDate As Date = FormatDateTime(DateTextBx.Text, 2)
        Dim Description As String = DescriptionTextBx.Text
        Dim PartDie As String = PtNoDropDn.SelectedValue

        Dim dbcon3 As New SqlConnection("Data Source=iowaeng;Persist Security Info=True;User ID=CLD;Password=cld;Initial Catalog=DieHistory")
        dbcon3.Open()
        Dim dbcmd3 As New SqlCommand
        dbcmd3.Connection = dbcon3
        dbcmd3.CommandText = "INSERT INTO Die_Change_History VALUES (" & Date & ", '" & Description & "', '" & PartDie & "')"

        dbcon3.Close()
        dbcmd3.Dispose()
        dbcon3.Dispose()


asp code:

<asp:Button ID="DieHistoryDone" runat="server" Text="Done" BackColor="#CCCCCC"
        BorderColor="Transparent" ForeColor="#003366" Font-Bold="True" Font-Size="Small"
        Width="25%" BorderStyle="Inset" />



<ajaxToolkit:ModalPopupExtender ID="ModalPopupExtender1" runat="server"
        TargetControlID="Button3" PopupControlID="Panel1">
    </ajaxToolkit:ModalPopupExtender>
0
rckrch
Asked:
rckrch
  • 6
  • 5
  • 2
  • +2
3 Solutions
 
slightwv (䄆 Netminder) Commented:
Maybe I'm missing something but shouldn't there be a dbcmd3.ExecuteNonQuery()
0
 
Evan CutlerCommented:
Your missing your execute command
            dbcmd3.ExecuteNonQuery()

For you tho, I'd also use a Try, Catch, Finally command (like below)
 

        Try
            con.Open()
            cmd.ExecuteNonQuery()
        Finally
            con.Close()
        End Try
0
 
rckrchAuthor Commented:
I get a syntax error on that line.

dbcmd3.ExecuteNonQuery()
0
VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

 
CodeCruiserCommented:
What's the type of date column?
0
 
rckrchAuthor Commented:
Thanks guys.  With the Syntax correction in the command it works - posting the data to the server.  However, now I have a problem with the date data in the column.  I have the Database column type as 'shortdate' and my date format in the code as 'mm/dd/yyyy' but it posts a date of 0001/1/1 00:00:00.

Any ideas?

Thanks verymuch for your help!!

0
 
slightwv (䄆 Netminder) Commented:
>>but it posts a date of

Posts to what?  Back to the page or database query?  My SQL Server is pretty weak.
0
 
rckrchAuthor Commented:
It posts to the sql server database a date of 1900-01-01 00:00:00.
0
 
Evan CutlerCommented:
that's ok...SQL Server just stores dates that way.
you have to format it in the U/I layer when the data comes back from the query.
if you just look at the data in SQL Server, that's how it looks.

You <OPERATIVE WORD> can </OPERATIVE WORD> change the format in the data base by setting the metadata in the database....but either way, when you get the date back in a SELECT query, you can format it in any method to be displayed on the website.
0
 
rckrchAuthor Commented:
Thanks for the reply arcee123.  However, I changing the column format to float and the value of the date code in the database for those dates is '0'.  Below is the copy of the database table in management studio.

2011-10-25 00:00:00      Trial      2261          
1900-01-01 00:00:00      Trial      2280          
1900-01-01 00:00:00      Trial      2280          
1900-01-01 00:00:00      Data trial 2      2261          
0
 
CodeCruiserCommented:
I would use a parameter

dbcmd3.CommandText = "INSERT INTO Die_Change_History VALUES (@thedate, '" & Description & "', '" & PartDie & "')"

dbcmd3.Parameters.AddWithValue("@thedate", Date)
0
 
Evan CutlerCommented:
your date column in the DB is a FLOAT?
0
 
Evan CutlerCommented:
if that's the case:
Dim ChangeDate As Date = FormatDateTime(DateTextBx.Text, 2)

with:
Dim ChangeDate As Float
ChangeDate = Format(DateSerial(DateTextBx.Text),"mmddyy")  


0
 
Evan CutlerCommented:
although by doing that your date functions disappear.
0
 
rckrchAuthor Commented:
Thanks codecruiser and thanks to everyone else.  The below code works perfectly.

dbcmd3.CommandText = "INSERT INTO Die_Change_History (Date, Description, KD) VALUES (@NewDate, '" & Description & "', '" & PartDie & "')"

        dbcmd3.Parameters.AddWithValue("@NewDate", ChangeDate)
0
 
rckrchAuthor Commented:
Thanks to all for helping with this problem.  I am still relatively new to asp and .net it is still taking me a while to recognize things that a veteran would not have trouble with.
0
 
Niladrisekhar DeyCommented:
There is a basic problem to insert the date into SQL server.  I always use a safe way - use dd-MMM-yyyy format and send it as a string.

Date Ex:
"insert into <table_name> (<date_field>) values ('" & Format(Date, "dd-MMM-yyyy") & "')"

Date & Time Ex:
"insert into <table_name> (<date_field>) values ('" & Format(Date, "dd-MMM-yyyy HH:mm:ss") & "')"

Do not forget the single quote - I've marked in bold
0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 6
  • 5
  • 2
  • +2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now