VB.NET : Trouble inserting data into database

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rashmidutt
    New Member
    • Mar 2008
    • 6

    VB.NET : Trouble inserting data into database

    hello sir
    i am making project on vb.net language..and project is on hospital management..its major project..and too many fields are present in its data base..i was connecting data base in forms but there is coming error..SIR PLEASE SORT OUT ERROR FROM MY COADING..what can i do to solve this error please give me sugession or if you can send me coading for adding information from form.than i will be thankfull to u..i have written all fields which has present in my database please help me..

    error coming - too many parameters.
    I HAVE MADE DATABASE ON MS ACCESS

    SOURCE CODE -
    [code=vbnet]
    Dim cmd As New OleDbCommand("i nsert into table1 values(@date,@f irstname,@middl ename,@lastname ,@maritalstatus ,@sex,@feetype,
    @mothersName,@f athersname,@dd, @mm,@yy,@age,@r esidentialaddre ss,@city,@state ,@country,@pinc ode,@religion,@ bloodgroup,@tel ephoneno, @mobile,@pager, @email)", con)

    cmd.parameters. addwithvalue("@ date", TextBox2.Text)
    cmd.Parameters. AddWithValue("@ firstname", TextBox14.Text)
    cmd.Parameters. AddWithValue("@ middlename", TextBox4.Text)
    cmd.Parameters. AddWithValue("@ lastname", TextBox5.Text)
    cmd.Parameters. AddWithValue("@ maritalstatus", ComboBox1.Text)
    cmd.Parameters. AddWithValue("@ sex", ComboBox2.Text)
    cmd.Parameters. AddWithValue("@ feetype", ComboBox3.Text)
    cmd.Parameters. AddWithValue("@ mothersname", TextBox6.Text)
    cmd.Parameters. AddWithValue("@ fathersname", TextBox7.Text)
    cmd.Parameters. AddWithValue("@ dd", ComboBox4.Text)
    cmd.Parameters. AddWithValue("@ mm", ComboBox5.Text)
    cmd.Parameters. AddWithValue("@ yy", ComboBox6.Text)
    cmd.Parameters. AddWithValue("@ age", TextBox8.Text)
    cmd.Parameters. AddWithValue("@ residentialaddr ess", TextBox9.Text)
    cmd.Parameters. AddWithValue("@ city", ComboBox7.Text)
    cmd.Parameters. AddWithValue("@ state", ComboBox8.Text)
    cmd.Parameters. AddWithValue("@ country", ComboBox9.Text)
    cmd.Parameters. AddWithValue("@ pincode", TextBox3.Text)
    cmd.Parameters. AddWithValue("@ religion", ComboBox10.Text )
    cmd.Parameters. AddWithValue("@ bloodgroup", ComboBox11.Text )
    cmd.Parameters. AddWithValue("@ telephoneno", TextBox11.Text)
    cmd.Parameters. AddWithValue("@ mobile", TextBox10.Text)
    cmd.Parameters. AddWithValue("@ pager", TextBox12.Text)
    cmd.Parameters. AddWithValue("@ email", TextBox13.Text)
    con.Open()
    cmd.ExecuteNonQ uery()
    con.Close()
    MsgBox("record added")

    End Sub[/code]
    Last edited by Frinavale; Mar 19 '08, 06:31 PM. Reason: added [code] tags
  • rashmidutt
    New Member
    • Mar 2008
    • 6

    #2
    help me

    sir i am making project on hospital management..the language is vb.net..i have tried to do databese connecting but there is coming errors..and the error is coming just because of too many parameters..so many fields are present in my database that is why it is not connecting..i am sendimg u fields i request to u if u can send me coading for adding information of form i will be thankfull to u please help me..
    fields is -
    date
    firstname
    middlename
    lastname, maritalstatus,s ex,feetype,moth ersName,fathers name,dd,mm,yy,a ge,residentiala ddress,city,sta te,country,pinc ode,religion,bl oodgroup,teleph oneno, mobile,pager,em ail

    its form's field name..and when we enter information in it..information should be save in database..so please send me coading of it.i have made database on ms access..u can see my coading which i have done on project..but its not working..

    [code=vbnet]
    Private Sub Button5_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button5.Click
    Dim cmd As New OleDbCommand("i nsert into table1 values(@date,@f irstname,@middl ename,@lastname ,@maritalstatus ,@sex,@feetype, @mothersName,@f athersname,@dd, @mm,@yy,@age,@r esidentialaddre ss,@city,@state ,@country,@pinc ode,@religion,@ bloodgroup,@tel ephoneno, @mobile,@pager, @email)", con)
    cmd.Parameters. AddWithValue("@ date", TextBox2.Text)
    cmd.Parameters. AddWithValue("@ firstname", TextBox14.Text)
    cmd.Parameters. AddWithValue("@ middlename", TextBox4.Text)
    cmd.Parameters. AddWithValue("@ lastname", TextBox5.Text)
    cmd.Parameters. AddWithValue("@ maritalstatus", ComboBox1.Text)
    cmd.Parameters. AddWithValue("@ sex", ComboBox2.Text)
    cmd.Parameters. AddWithValue("@ feetype", ComboBox3.Text)
    cmd.Parameters. AddWithValue("@ mothersname", TextBox6.Text)
    cmd.Parameters. AddWithValue("@ fathersname", TextBox7.Text)
    cmd.Parameters. AddWithValue("@ dd", ComboBox4.Text)
    cmd.Parameters. AddWithValue("@ mm", ComboBox5.Text)
    cmd.Parameters. AddWithValue("@ yy", ComboBox6.Text)
    cmd.Parameters. AddWithValue("@ age", TextBox8.Text)
    cmd.Parameters. AddWithValue("@ residentialaddr ess", TextBox9.Text)
    cmd.Parameters. AddWithValue("@ city", ComboBox7.Text)
    cmd.Parameters. AddWithValue("@ state", ComboBox8.Text)
    cmd.Parameters. AddWithValue("@ country", ComboBox9.Text)
    cmd.Parameters. AddWithValue("@ pincode", TextBox3.Text)
    cmd.Parameters. AddWithValue("@ religion", ComboBox10.Text )
    cmd.Parameters. AddWithValue("@ bloodgroup", ComboBox11.Text )
    cmd.Parameters. AddWithValue("@ telephoneno", TextBox11.Text)
    cmd.Parameters. AddWithValue("@ mobile", TextBox10.Text)
    cmd.Parameters. AddWithValue("@ pager", TextBox12.Text)
    cmd.Parameters. AddWithValue("@ email", TextBox13.Text)
    con.Open()
    cmd.ExecuteNonQ uery()
    con.Close()
    MsgBox("record added")

    End Sub[/code]
    thanking u
    Last edited by debasisdas; Mar 17 '08, 05:51 AM. Reason: Question moved to .NET forum from articles.

    Comment

    • VBWheaties
      New Member
      • Feb 2008
      • 145

      #3
      Data integrity is probably the issue. Make sure the values typed in the textboxes are allowed for the underlying data fields datatype. For example, if it is expecting a number, textboxes all return string values and it will error out.

      Also, I would get rid of command objects in this case. You just dont need them for what you are doing.

      Create a single INSERT statement like you did but remove the "@" place holders and put actual values.

      For example:

      INSERT INTO MyTable (Field1, Field2, Field3)
      VALUES ('Value of field1', 'Value of field2', 'Value of field 3')
      Last edited by VBWheaties; Mar 14 '08, 07:41 PM. Reason: removed quoted text as it is above this post

      Comment

      • Plater
        Recognized Expert Expert
        • Apr 2007
        • 7872

        #4
        Originally posted by VBWheaties
        Also, I would get rid of command objects in this case. You just dont need them for what you are doing.
        I dunno, using a command object like that is safer. It forces correct datatypes and helps prevent against sql injection.


        As for the problem, I don't think that is a valid SQL INSERT statement?

        Comment

        • VBWheaties
          New Member
          • Feb 2008
          • 145

          #5
          Originally posted by Plater
          I dunno, using a command object like that is safer. It forces correct datatypes and helps prevent against sql injection.


          As for the problem, I don't think that is a valid SQL INSERT statement?
          You are right. In this case, it's a toss up between letting the code handle exceptions or, what I like to do, check the data integrity before sending it to the db.

          Comment

          • balabaster
            Recognized Expert Contributor
            • Mar 2007
            • 798

            #6
            Originally posted by VBWheaties
            You are right. In this case, it's a toss up between letting the code handle exceptions or, what I like to do, check the data integrity before sending it to the db.
            The problems are:

            a). In your SQL statement you don't specify the fields to insert into - consequently you are required to enter all fields for the table you're inserting into. This means that if extra fields are added in your database, your query will then crash, which is what I suspect is happening to you. Try the following format:

            Code:
            Dim sSql As String = "Insert Into MyTable(Field1, Field2, Field3) Values(@Data1, @Data2, @Data3)"
            Dim oCmd As New SqlCommand(sSQL, oCon)
            b). Your SqlParameter instances don't need the @ symbol when you're creating your parameter object:

            Code:
            oCmd.Parameters.AddWithValue("Data1", TextBox1.Text)
            oCmd.Parameters.AddWithValue("Data2", TextBox2.Text)
            oCmd.Parameters.AddWithValue("Data3", TextBox3.Text)
            Notice how in my SQL statement I specify the placeholders using the @ symbol, but when I create my SqlParameter instances, I don't use the @ symbol.

            If you correct both of these issues in your code, it should fix your problem.

            Comment

            • debasisdas
              Recognized Expert Expert
              • Dec 2006
              • 8119

              #7
              Problem is not with number of parameters.

              Which line of code is creating the error ?

              Comment

              • rashmidutt
                New Member
                • Mar 2008
                • 6

                #8
                Inserting data from a textbox into a database

                hello sir

                i want to know,after inserting the data in the textbox, how can we add that data in the database with the click of a button?
                sir i have written code for this work please correct my coding..
                error coming - "Data type mismatch in criteria expression"

                Private Sub Button5_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button5.Click

                cmd.Parameters. AddWithValue("@ date", TextBox2.Text)
                cmd.Parameters. AddWithValue("@ uhid.textbox1.t ext)
                cmd.Parameters. AddWithValue("@ first name,TextBox14. Text)
                cmd.Parameters. AddWithValue("@ middle name",TextBox4. Text)
                cmd.Parameters. AddWithValue( "@last name",TextBox5. Text)
                cmd.Parameters. AddWithValue("@ marital status",ComboBo x1.Text)
                cmd.Parameters. AddWithValue("@ sex",ComboBox2. Text)
                cmd.Parameters. AddWithValue("@ fee type",ComboBox3 .Text)
                cmd.Parameters. AddWithValue("@ mother's Name",TextBox6. Text)
                cmd.Parameters. AddWithValue("@ father's name",TextBox7. Text)
                cmd.Parameters. AddWithValue("@ dd",ComboBox4.T ext)
                cmd.Parameters. AddWithValue("@ mm",ComboBox5.T ext)
                cmd.Parameters. AddWithValue("@ yy",ComboBox6.T ext)
                cmd.Parameters. AddWithValue("@ age",TextBox8.T ext)
                cmd.Parameters. AddWithValue("@ residential address"TextBox 9.Text)
                cmd.Parameters. AddWithValue("@ city"combobox7. text)
                cmd.Parameters. AddWithValue("@ state",combobox 8.text)
                cmd.Parameters. AddWithValue ("@country",com bobox9.text)
                cmd.Parameters. AddWithValue("@ pincode""TextBo x3.Text)
                cmd.Parameters. AddWithValue("@ region",combox1 0.text)
                cmd.Parameters. AddWithValue ("@blood group",combobox 11.text)
                cmd.Parameters. AddWithValue("@ telephone no",textbox11.t ext)
                cmd.Parameters. AddWithValue ("@mobile",text box10.text)
                cmd.Parameters. AddWithValue("@ pager",TextBox1 2.Text)
                cmd.Parameters. AddWithValue("@ email",textbox1 3.text)
                con.Open()
                cmd.ExecuteNonQ uery()
                con.Close()
                MsgBox("record added")

                End Sub

                Comment

                • rashmidutt
                  New Member
                  • Mar 2008
                  • 6

                  #9
                  please sort out my problem

                  hello sir
                  i want to know after inserting data in the textbox than how can we add this data to the database.with the help of click of button.

                  sir i have written coad for this work please check the coding

                  error coming - Data type mismatch in criteria expression

                  please solve my problem

                  source code -
                  [code=vbnet]
                  Private Sub Button5_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button5.Click

                  cmd.Parameters. AddWithValue("@ date", TextBox2.Text)
                  cmd.Parameters. AddWithValue("@ uhid.textbox1.t ext)
                  cmd.Parameters. AddWithValue("@ first name,TextBox14. Text)
                  cmd.Parameters. AddWithValue("@ middle name",TextBox4. Text)
                  cmd.Parameters. AddWithValue( "@last name",TextBox5. Text)
                  cmd.Parameters. AddWithValue("@ marital status",ComboBo x1.Text)
                  cmd.Parameters. AddWithValue("@ sex",ComboBox2. Text)
                  cmd.Parameters. AddWithValue("@ fee type",ComboBox3 .Text)
                  cmd.Parameters. AddWithValue("@ mother's Name",TextBox6. Text)
                  cmd.Parameters. AddWithValue("@ father's name",TextBox7. Text)
                  cmd.Parameters. AddWithValue("@ dd",ComboBox4.T ext)
                  cmd.Parameters. AddWithValue("@ mm",ComboBox5.T ext)
                  cmd.Parameters. AddWithValue("@ yy",ComboBox6.T ext)
                  cmd.Parameters. AddWithValue("@ age",TextBox8.T ext)
                  cmd.Parameters. AddWithValue("@ residential address"TextBox 9.Text)
                  cmd.Parameters. AddWithValue("@ city"combobox7. text)
                  cmd.Parameters. AddWithValue("@ state",combobox 8.text)
                  cmd.Parameters. AddWithValue ("@country",com bobox9.text)
                  cmd.Parameters. AddWithValue("@ pincode""TextBo x3.Text)
                  cmd.Parameters. AddWithValue("@ region",combox1 0.text)
                  cmd.Parameters. AddWithValue ("@blood group",combobox 11.text)
                  cmd.Parameters. AddWithValue("@ telephone no",textbox11.t ext)
                  cmd.Parameters. AddWithValue ("@mobile",text box10.text)
                  cmd.Parameters. AddWithValue("@ pager",TextBox1 2.Text)
                  cmd.Parameters. AddWithValue("@ email",textbox1 3.text)
                  con.Open()
                  cmd.ExecuteNonQ uery()
                  con.Close()
                  MsgBox("record added")

                  End Sub[/code]
                  Last edited by Frinavale; Mar 19 '08, 06:31 PM. Reason: added [code] tags

                  Comment

                  • rashmidutt
                    New Member
                    • Mar 2008
                    • 6

                    #10
                    solve the code please

                    hello sir
                    i want to know after inserting data in the textbox than how can we add this data to the database.with the help of click of button.
                    i m making project in visual studio 2005
                    backend - ms access

                    sir i have written coad for this work please check the coding

                    error coming - Data type mismatch in criteria expression

                    please solve my problem

                    source code -
                    [code=vbnet]
                    Private Sub Button5_Click(B yVal sender As System.Object, ByVal e As System.EventArg s) Handles Button5.Click

                    cmd.Parameters. AddWithValue("@ date", TextBox2.Text)
                    cmd.Parameters. AddWithValue("@ uhid.textbox1.t ext)
                    cmd.Parameters. AddWithValue("@ first name,TextBox14. Text)
                    cmd.Parameters. AddWithValue("@ middle name",TextBox4. Text)
                    cmd.Parameters. AddWithValue( "@last name",TextBox5. Text)
                    cmd.Parameters. AddWithValue("@ marital status",ComboBo x1.Text)
                    cmd.Parameters. AddWithValue("@ sex",ComboBox2. Text)
                    cmd.Parameters. AddWithValue("@ fee type",ComboBox3 .Text)
                    cmd.Parameters. AddWithValue("@ mother's Name",TextBox6. Text)
                    cmd.Parameters. AddWithValue("@ father's name",TextBox7. Text)
                    cmd.Parameters. AddWithValue("@ dd",ComboBox4.T ext)
                    cmd.Parameters. AddWithValue("@ mm",ComboBox5.T ext)
                    cmd.Parameters. AddWithValue("@ yy",ComboBox6.T ext)
                    cmd.Parameters. AddWithValue("@ age",TextBox8.T ext)
                    cmd.Parameters. AddWithValue("@ residential address"TextBox 9.Text)
                    cmd.Parameters. AddWithValue("@ city"combobox7. text)
                    cmd.Parameters. AddWithValue("@ state",combobox 8.text)
                    cmd.Parameters. AddWithValue ("@country",com bobox9.text)
                    cmd.Parameters. AddWithValue("@ pincode""TextBo x3.Text)
                    cmd.Parameters. AddWithValue("@ region",combox1 0.text)
                    cmd.Parameters. AddWithValue ("@blood group",combobox 11.text)
                    cmd.Parameters. AddWithValue("@ telephone no",textbox11.t ext)
                    cmd.Parameters. AddWithValue ("@mobile",text box10.text)
                    cmd.Parameters. AddWithValue("@ pager",TextBox1 2.Text)
                    cmd.Parameters. AddWithValue("@ email",textbox1 3.text)
                    con.Open()
                    cmd.ExecuteNonQ uery()
                    con.Close()
                    MsgBox("record added")

                    End Sub[/code]
                    Last edited by Frinavale; Mar 19 '08, 06:40 PM. Reason: added [code] tags

                    Comment

                    • nateraaaa
                      Recognized Expert Contributor
                      • May 2007
                      • 664

                      #11
                      The type you pass to your stored procedure must match the type for the parameter. You are getting this error because you are passing a string when your proc expects a different type. You will need to Convert the text from your textboxes to match the type that your parameter expects.

                      Code:
                       ("@Parameter1", Convert.ToInt32(TextBox1.Text);
                      Nathan

                      Comment

                      • nateraaaa
                        Recognized Expert Contributor
                        • May 2007
                        • 664

                        #12
                        Please do not post duplicate questions. This violates the posting guidelines.


                        Nathan

                        Comment

                        • ShahbazAshraf
                          New Member
                          • Mar 2008
                          • 36

                          #13
                          Just match the type with your database scheme ...

                          Comment

                          • Frinavale
                            Recognized Expert Expert
                            • Oct 2006
                            • 9749

                            #14
                            Originally posted by nateraaaa
                            Please do not post duplicate questions. This violates the posting guidelines.


                            Nathan
                            Please read the Posting guidelines. Specifically the section on Do not double post your question.

                            I have merged all of your duplicate threads into this one and I am closing this thread.

                            You can see the solution to your problem in your other thread that has the answers. That is where you can continue to receive help on your problem.

                            -Moderator Frinny

                            Comment

                            • Plater
                              Recognized Expert Expert
                              • Apr 2007
                              • 7872

                              #15
                              I split off your question from the articles section (do not post questions there) and merged a number of your similar threads. Please don't post the same question over and over again.


                              MODERATOR

                              Comment

                              Working...