Click here to Skip to main content
15,888,454 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Please see the Below mentioned query, the problem is, I want insert the data into the table but nothing inserted, Please solve this problem.

VB
Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

        Dim insql As String = "INSERT INTO patient (recpno,name,age,phone,date,refby,address,testcode,testname,amount) VALUES (@number1,@text2,@number3,@number4,@text5,@text6,@text7,@text8,@text9,@number10)"

        Dim cmd As SqlCommand = New SqlCommand(insql, sqlconn)
        Try

            sqlconn.Open()

            cmd.Parameters.AddWithValue("@number1", TextBox1.Text)
            cmd.Parameters.AddWithValue("@text2", TextBox2.Text)
            cmd.Parameters.AddWithValue("@number3", TextBox3.Text)
            cmd.Parameters.AddWithValue("@number4", TextBox4.Text)
            cmd.Parameters.AddWithValue("@text5", TextBox5.Text)
            cmd.Parameters.AddWithValue("@text6", TextBox6.Text)
            cmd.Parameters.AddWithValue("@text7", TextBox7.Text)
            cmd.Parameters.AddWithValue("@text8", TextBox8.Text)
            cmd.Parameters.AddWithValue("@text9", TextBox9.Text)
            cmd.Parameters.AddWithValue("@number10", TextBox10.Text)


            Dim rowsins As Double = cmd.ExecuteNonQuery

            MessageBox.Show(rowsins.ToString & rowsins.ToString & "row added")
            sqlconn.Close()
        Catch ex As Exception

        End Try
    End Sub
End Class
Posted
Updated 6-Feb-11 20:37pm
v2
Comments
TweakBird 7-Feb-11 2:37am    
Edited for code blocks. Please use <pre> tag for code blocks.

Try debugging your code to see all your textboxes have legitimate values.

Pick up the query and use the parameters set here to write a Select query. Execute this query to ensure that a row really exists for this data.
 
Share this answer
 
I will try again but same thing is happen

VB
Dim insql As String = "INSERT INTO patient (recpno,name,age,phone,date,refby,address,testcode,testname,amount) VALUES (@number1,@text2,@number3,@text4,@text5,@number6,@text7,@text8,@text9,@number10)"

        Dim cmd As SqlCommand = New SqlCommand(insql, sqlconn)
        Try

            sqlconn.Open()

            cmd.Parameters.AddWithValue("@number1", recieptxt.Text)
            cmd.Parameters.AddWithValue("@text2", nametxt.Text)
            cmd.Parameters.AddWithValue("@number3", agetxt.Text)
            cmd.Parameters.AddWithValue("@text", addresstxt.Text)
            cmd.Parameters.AddWithValue("@text5", datetxt.Text)
            cmd.Parameters.AddWithValue("@number6", phonetxt.Text)
            cmd.Parameters.AddWithValue("@text7", refbytxt.Text)
            cmd.Parameters.AddWithValue("@text8", testcodetxt.Text)
            cmd.Parameters.AddWithValue("@text9", testnametxt.Text)
            cmd.Parameters.AddWithValue("@number10", amounttxt.Text)


            Dim rowsins As Double = cmd.ExecuteNonQuery
              sqlconn.Close()

        Catch ex As Exception
            MessageBox.Show(rowsins.ToString & rowsins.ToString & " row added")
        End Try
   End Sub
 
Share this answer
 
Comments
Abhinav S 7-Feb-11 3:33am    
Are you getting an error? If so what error?
Mudasar Ahmad 7-Feb-11 3:37am    
I am getting any error that is why i am confused, please solve this
Abhinav S 7-Feb-11 3:42am    
Try looking at all the values in the textboxes. Make sure they have correct values. Try running this query (as a Select query) in the backend to ensure there is a record with these parameters.
Mudasar Ahmad 7-Feb-11 3:54am    
please check this query give me the solution
there's nothing wrong with your code. most probably you got the wrong sqlconn connection string or you got the wrong table.

to see the exception. rewrite to exception catch like this:

Catch ex as Exception
msgbox(ex.message)
End Try
 
Share this answer
 
Comments
Mudasar Ahmad 8-Feb-11 3:18am    
sir it shows the message 00rows added
ulyses31 9-Feb-11 0:35am    
you need to replace your exception catch messagebox with (ex.message). this will show the exception message not your rows added.
It is extremely difficult for anyone to check your code without a configured database and connection string to match.

You want a quick solution? Remove the try/catch completely. That is the best way to see what is causing the problem.

Dim cmd As SqlCommand = New SqlCommand(insql, sqlconn)        
sqlconn.Open()
cmd.Parameters.AddWithValue("@number1", recieptxt.Text)
cmd.Parameters.AddWithValue("@text2", nametxt.Text)
cmd.Parameters.AddWithValue("@number3", agetxt.Text)            cmd.Parameters.AddWithValue("@text", addresstxt.Text)            cmd.Parameters.AddWithValue("@text5", datetxt.Text)            cmd.Parameters.AddWithValue("@number6", phonetxt.Text)            cmd.Parameters.AddWithValue("@text7", refbytxt.Text)            cmd.Parameters.AddWithValue("@text8", testcodetxt.Text)            cmd.Parameters.AddWithValue("@text9", testnametxt.Text)            cmd.Parameters.AddWithValue("@number10", amounttxt.Text)
Dim rowsins As Double = cmd.ExecuteNonQuery
sqlconn.Close()        
 
Share this answer
 
Comments
Dalek Dave 8-Feb-11 4:39am    
Nice.
The problem with empty catch blocks is that you never find you have a problem.

Try putting a MessageBox.Show into the catch block and looking at the message and any inner exception. There is a good chance that the SQL is complaining about a syntax error, type error or similar.

BTW: Please, for your own benefit (as well as that of anyone who has to look at your code) use sensible names! TextBox1 to TextBox10 is not descriptive of what they hold, and it is hard to be sure you are putting the right data in the right field.

The same goes for named parameters: you have gone to the effort to name your table fields sensibly, so why not use the same names for your parameters? Then you could see at a glance if they match up...
 
Share this answer
 
Comments
Abhinav S 7-Feb-11 3:26am    
Not sure why you got downvoted. Sound advice. 5.
Dalek Dave 8-Feb-11 4:38am    
Good Call

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900