Results 1 to 14 of 14

Thread: INSERT INTo Command

  1. #1

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    INSERT INTo Command

    Hi,

    I am working with asp.net and VB.Net;

    What am I doing wrong please?

    Code:
     Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
    
    
            Dim strConn As String
            strConn = ConfigurationManager.ConnectionStrings("CCMSConnectionString").ConnectionString
    
            Dim conn As New SqlConnection(strConn)
    
            conn.Open()
    
            Dim strSql As String
    
            strSql = "INSERT INTO dbo.TblChangeControlDet (RequestBy, CreatedBy, ChangeType, TechnicalPerson, Req, BusImp,ChangeRaisedDate, PeerRvw) " & _
                                                "VALUES (@RequestBy, @CreatedBy, @ChangeType, @TechnicalPerson, @Req, @BusImp,@ChangeRaisedDate, @PeerRvw )"
    
            Dim cmd As New SqlCommand(strSql, conn)
    
            cmd.Parameters.AddWithValue("@RequestBy", Me.cmbchngreq.Text)
            cmd.Parameters.AddWithValue("@CreatedBy", Me.cmbchngcrt.Text)
            cmd.Parameters.AddWithValue("@ChangeType", Me.cmbchngtyp.Text)
            cmd.Parameters.AddWithValue("@TechnicalPerson", Me.cmbTechname.Text)
            cmd.Parameters.AddWithValue("@Req", Me.txtreq.Text)
            cmd.Parameters.AddWithValue("@BusImp", Me.txtbusimp.Text)
            cmd.Parameters.AddWithValue("@ChangeRaisedDate", Me.txtdate.Text)
            cmd.Parameters.AddWithValue("@PeerRvw", Me.cmbpeername.Text)
    
            conn.Close()
    
        End Sub
    Database is not updated even if data in inserted

    Many many thanks
    Last edited by dr223; Oct 18th, 2012 at 09:55 AM.

  2. #2
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: INSERT INTo Command

    What am I doing wrong please?
    Asking non-questions for a start. What's the actual problem? Are you getting errors?
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  3. #3
    Super Moderator Shaggy Hiker's Avatar
    Join Date
    Aug 2002
    Location
    Idaho
    Posts
    40,104

    Re: INSERT INTo Command

    Are you getting errors, or just getting nothing?
    My usual boring signature: Nothing

  4. #4

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: INSERT INTo Command

    No Errors - but no data is added to the database..
    Whe th ebutton is CLICKED..

  5. #5
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: INSERT INTo Command

    Probably because you never actually execute the command. You're missing a cmd.ExecuteNonQuery
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  6. #6

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: INSERT INTo Command

    where should I put it please

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: INSERT INTo Command

    cmd.Parameters.AddWithValue("@PeerRvw", Me.cmbpeername.Text)

    cmd.ExecuteNonQuery

    conn.Close()
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  8. #8

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: INSERT INTo Command

    Thnks; Ok this inserts data into the database i.e., the connection is Fine.

    Code:
       Protected Sub btnsubmit_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnsubmit.Click
    
    
            Dim strConn As String
    
            strConn = ConfigurationManager.ConnectionStrings("CCMSConnectionString").ConnectionString
    
            Dim conn As New SqlConnection(strConn)
    
            conn.Open()
    
            Dim strSql As String
    
            strSql = "INSERT INTO dbo.TblChangeControlDet (RequestBy, CreatedBy, ChangeType, TechnicalPerson, Req, BusImp,ChangeRaisedDate, PeerRvw) VALUES (@RequestBy, @CreatedBy, @ChangeType, @TechnicalPerson, @Req, @BusImp,@ChangeRaisedDate, @PeerRvw )"
    
            Dim cmd As New SqlCommand(strSql, conn)
    
            With cmd.Parameters
                .AddWithValue("@RequestBy", Me.cmbchngreq.Text)
                .AddWithValue("@CreatedBy", Me.cmbchngcrt.Text)
                .AddWithValue("@ChangeType", Me.cmbchngtyp.Text)
                .AddWithValue("@TechnicalPerson", Me.cmbTechname.Text)
                .AddWithValue("@Req", Me.txtreq.Text)
                .AddWithValue("@BusImp", Me.txtbusimp.Text)
                .AddWithValue("@ChangeRaisedDate", Me.txtdate.Text)
                .AddWithValue("@PeerRvw", Me.cmbpeername.Text)
            End With
    
            cmd.ExecuteNonQuery()
    
            conn.Close()
    
        End Sub
    However, the data is incorrect..

    RequestBy = -1 (Expected and item selected which are usually names e.g., John Smith etc)
    CreatedBy = -1 (Expected and item selected which are usually names e.g., John Smith etc)
    ChangeType = -1 (Expected and item selected which are usually natypes e.g websites)
    TechnicalPerson = -1 (Expected and item selected which are usually names e.g., John Smith etc)
    Req = Empty (eventhough I typed data before clicking the button)
    BusImp = Empty (eventhough I typed data before clicking the button)
    ChangeRaisedDate = 18/10/12 (this was the Only correct data)
    PeerRvw = -1

    Why is it behaving like so.

    Note:

    My first field is set as Please Select set in asp as

    Code:
      <asp:ListItem Value="-1">Please Select</asp:ListItem>
    Please help me.

    Secondly when I click submit button it refreshes all the fields, why? Nothing in the click event states so...

    Many thanks

  9. #9
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: INSERT INTo Command

    Assuming that this is a combobox ...

    Me.cmbchngreq.Text

    ..... you should be using .SelectedItem.ToString not .Text

    As for the empty values, are the controls actually on the same form as this code? If not, then it can't be Me.!
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  10. #10

    Thread Starter
    Frenzied Member
    Join Date
    Feb 2009
    Posts
    1,058

    Re: INSERT INTo Command

    Ok Changed to:

    Code:
    With cmd.Parameters
                .AddWithValue("@RequestBy", Me.cmbchngreq.SelectedItem.ToString)
                .AddWithValue("@CreatedBy", Me.cmbchngcrt.SelectedItem.ToString)
                .AddWithValue("@ChangeType", Me.cmbchngtyp.SelectedItem.ToString)
                .AddWithValue("@TechnicalPerson", Me.cmbTechname.SelectedItem.ToString)
                .AddWithValue("@Req", txtreq.Text)
                .AddWithValue("@BusImp", txtbusimp.Text)
                .AddWithValue("@ChangeRaisedDate", Me.txtdate.Text)
                .AddWithValue("@PeerRvw", Me.cmbpeername.SelectedItem.ToString)
            End With
    RequestBy = Please Select
    CreatedBy = Please Select
    ChangeType = Please Select
    Technical Person = Please Select
    Req = Empty
    BusImp = Empty
    PeerRvw = Please Select

    Even, when I select a different value on the combo boxes and click the button. It still takes the value Please Select and for the emoty textboxes I cant even explain

    Any ideas..

    Many thanks

  11. #11
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 2012
    Posts
    8,245

    Re: INSERT INTo Command

    It's going to be in the way that you're linking the controls from ASP which I guess means looking at your design as well as the code. Can you zip your project and attach it here? I can't promise that I'll get to look at it immediately but somewhen between now and the end of the world .....
    As the 6-dimensional mathematics professor said to the brain surgeon, "It ain't Rocket Science!"

    Reviews: "dunfiddlin likes his DataTables" - jmcilhinney

    Please be aware that whilst I will read private messages (one day!) I am unlikely to reply to anything that does not contain offers of cash, fame or marriage!

  12. #12
    PowerPoster techgnome's Avatar
    Join Date
    May 2002
    Posts
    34,687

    Re: INSERT INTo Command

    I suspect that your button is autoposting... so it's causing a refresh and when the code is running, the data hasn't been set in the form elements yet, so you're getting the default values.

    I'd also suggest this get moved to the ASP.NET forums, as it's a problem with the ASP.NET setup and not about the database (at least not any more). I'll ask as mod to move it.

    -tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  13. #13
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: INSERT INTo Command

    Moved

  14. #14
    King of sapila
    Join Date
    Oct 2006
    Location
    Greece
    Posts
    6,763

    Re: INSERT INTo Command

    I'm close to TG opinion but i suspect your dropdownlist is autoposting.Show your markup.
    ἄνδρα μοι ἔννεπε, μοῦσα, πολύτροπον, ὃς μάλα πολλὰ
    πλάγχθη, ἐπεὶ Τροίης ἱερὸν πτολίεθρον ἔπερσεν·

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width