Click here to Skip to main content
15,914,225 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
VB
con.Open()
        If TextBox1.Text = "" And TextBox2.Text = "" Then
            MsgBox("Please Enter A Value", vbInformation)
        Else
            qur = "Insert Into EXA values('" + TextBox1.Text + "','" + TextBox2.Text + "')"
            Dim cmd = New OleDbCommand
            cmd = New OleDbCommand(qur, con)
            cmd.ExecuteNonQuery()
            dt = New DataTable()
            oAdapter = New OleDbDataAdapter(qur, con)
            oAdapter.Fill(dt)
            DataGridView1.DataSource = dt
            grid_dis()
        End If
        con.Close()



this is the code for inserting a value in oracle database
it have to inserts one value in databse but its inserts two values in database plzz help me
Posted

1 solution

Well, it could be that you are using the same INSERT query for your OleDbCommand.ExecuteNonQuery and for your OldDataAdapter.Fill - I'm pretty sure that if you replace data adapter command with a SELECT query, things will work rather better... :laugh:

BTW: Do not concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Use Parametrized queries instead - it's a lot, lot safer!


"will you explain how to use parametrized queries plzzz...."

I will ignore the "replay fast" instruction - this one time, and one time only. Any future usages will get you no further response from me. It is rude, arrogant and unnecessary, and all it will do is annoy people. Please consider the possible effects of what you are about to say before you send or post a message!

VB
Using con As New SqlConnection(strConnect)
	con.Open()
	Using com As New SqlCommand("INSERT INTO EXA (myColumn1, myColumn2) VALUES (@C1, @C2)", con)
		com.Parameters.AddWithValue("@C1", TextBox1.Text)
		com.Parameters.AddWithValue("@C2", TextBox2.Text)
		com.ExecuteNonQuery()
	End Using
End Using
In addition, you should always list the columns you want to insert data into (I called them myColumn1 and myColumn2) - it helps prevent mistakes if the database is changed at a later date, and makes the code more readable. You can use any name for the parameters - I used C1 and C2 because I have no idea what the text boxes contain! (See what I mean about readability?)

And please, stop using Visual Studio default names for controls! Calling them "tbUserName" and "tbAddress" or whatever instead of "Textbox1" and "TextBox2" makes your code more readable, and it can be quicker to type because Intellisense can figure it out for you...
 
Share this answer
 
v2
Comments
vickeysanlge 29-Sep-13 12:02pm    
thank you very much....
OriginalGriff 29-Sep-13 12:07pm    
You're welcome!
OriginalGriff 29-Sep-13 12:15pm    
Answer updated.
bluesathish 30-Sep-13 3:40am    
@OriginalGriff, Good solution griff keep it up. :-) My vote 5!.
vickeysanlge 29-Sep-13 12:20pm    
sorry for that repaly fast...
plzz help it is giving error to strConnect

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