Results 1 to 14 of 14

Thread: INSERT INTo Command

  1. #1
    Fanatic Member
    Join Date
    Feb 09
    Posts
    819

    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 12
    Posts
    5,546

    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!"

    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
    Loquacious User Shaggy Hiker's Avatar
    Join Date
    Aug 02
    Location
    Idaho
    Posts
    20,410

    Re: INSERT INTo Command

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

  4. #4
    Fanatic Member
    Join Date
    Feb 09
    Posts
    819

    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 12
    Posts
    5,546

    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!"

    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
    Fanatic Member
    Join Date
    Feb 09
    Posts
    819

    Re: INSERT INTo Command

    where should I put it please

  7. #7
    PowerPoster dunfiddlin's Avatar
    Join Date
    Jun 12
    Posts
    5,546

    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!"

    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
    Fanatic Member
    Join Date
    Feb 09
    Posts
    819

    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 12
    Posts
    5,546

    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!"

    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
    Fanatic Member
    Join Date
    Feb 09
    Posts
    819

    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 12
    Posts
    5,546

    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!"

    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 02
    Posts
    21,659

    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 subscribe to all threads I participate, so there's no need to pm when there's an update.*
    *Proof positive that searching the forums does work: View Thread *
    * 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??? *
    * Use Offensive Programming, not Defensive Programming. * On Error Resume Next is error ignoring, not error handling(tm).
    "There is a major problem with your code, and VB wants to tell you what it is.. but you have decided to put your fingers in your ears and shout 'I'm not listening!'" - si_the_geek on using OERN

  13. #13
    Super Moderator Hack's Avatar
    Join Date
    Aug 01
    Location
    Searching for mendhak
    Posts
    58,283

    Re: INSERT INTo Command

    Moved
    Please use [Code]your code goes in here[/Code] tags when posting code.
    When you have received an answer to your question, please mark it as resolved using the Thread Tools menu.
    Before posting your question, did you look here?
    Got a question on Linux? Visit our Linux sister site.
    I dont answer coding questions via PM or EMail. Please post a thread in the appropriate forum section.

    Creating A Wizard In VB.NET
    Paging A Recordset
    What is wrong with using On Error Resume Next
    Good Article: Language Enhancements In Visual Basic 2010
    Upgrading VB6 Code To VB.NET
    Microsoft MVP 2005/2006/2007/2008/2009/2010/2011/2012/Defrocked

  14. #14
    King of sapila
    Join Date
    Oct 06
    Location
    Greece
    Posts
    3,521

    Re: INSERT INTo Command

    I'm close to TG opinion but i suspect your dropdownlist is autoposting.Show your markup.
    Slow as hell.

Posting Permissions

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