save data that is entered

pri1493

Member
Joined
Feb 16, 2013
Messages
10
Programming Experience
Beginner
i need to save data that is entered at run time how do i do that? i tried lots of codes, i do not get any errors in my code but the data is not present in the database wen i exit and check my data table. please help

here is my code:

Dim con As New SqlClient.SqlConnection
Dim cmd As New SqlClient.SqlCommand


Try
con.ConnectionString = "Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\ClubDatabase.mdf;Integrated Security=True;User Instance=True"
con.Open()
cmd.Connection = con
cmd.CommandText = "INSERT INTO Liquor([Product ID], [Name], [Quantity], [Cost Price], [Selling Price]) VALUES('" & Product_IDTextBox.Text & "','" & NameTextBox.Text & "','" & QuantityTextBox.Text & "','" & Cost_PriceTextBox.Text & "','" & Selling_PriceTextBox.Text & "')"
cmd.ExecuteNonQuery()
MessageBox.Show("added")
Catch ex As Exception
MessageBox.Show("Error while inserting record on table..." & ex.Message, "Insert Records")
Finally
con.Close()
End Try
 
Are you sure you are looking in the right database?
The database where your insert is executed is in the bin/debug or bin/release directory.

Furthermore: if you added the database file to your project, check the property "Copy to Output Directory".
If it says "Copy Always", the database in the output directory (bin/debug or bin/release) is overwritten with each build of your project.
 
Something along the lines of:

VB.NET:
cmd.CommandText ="DELETE FROM Liquor WHERE [Product Id] = @Id"
cmd.Parameters.AddWithValue("Id", productId )
cmd.ExecuteNonQuery

where productId is the [Product Id] you want to remove.


Do not concatenate your SQL strings, always use Parameter objects .
 
Thank u that was really helpful

Something along the lines of:

VB.NET:
cmd.CommandText ="DELETE FROM Liquor WHERE [Product Id] = @Id"
cmd.Parameters.AddWithValue("Id", productId )
cmd.ExecuteNonQuery

where productId is the [Product Id] you want to remove.


Do not concatenate your SQL strings, always use Parameter objects .

is it possible to specify table name at run time ???
 
is it possible to specify table name at run time ???
Yes, it is.
VB.NET:
Const DeleteSQL As String = "DELETE FROM {0} WHERE {1} = @PK"
cmd.CommandText = String.Format(DeleteSQL, tableName, pkName)
' ...

But this is rarely really necessary.
You could also define several Const strings (one for each table) for the delete statements.
VB.NET:
Const DeletelLiquorSQL As String = "DELETE FROM Liquor WHERE [Prodcut Id] = @Id"
Const DeletelMemberSQL As String = "DELETE FROM Member WHERE [Name] = @Name"
' ...

Also: have a look at the SqlCommandBuilder class.
And while you're at it: Typed DataSets.
 
First of all thank u so so so much again... this code might just work


i am assuming that the code given below is correct... correct me if m wrong

VB.NET:
tableName= textbox1.tex
tpkname= textbox2.text
Const DeleteSQL As String = "DELETE FROM {0} WHERE {1} = @PK"
cmd.CommandText = String.Format(DeleteSQL, tableName, pkName)
 
I do and can not know if this code is correct for your purpose.
I have no idea what the content of these textboxes is.
If TextBox1.Text contains the name of your table and TextBox2.Text contains the name of the primary key or another identifying column in that table, then: yes this is correct.
Do not forget to add the value for the parameter @PK with cmd.Parameters.AddWithValue("PK", <your value>)
 
Thanks Again

The code that u gave me works perfectly according to my requirement...
Now i have just 2 more problems, hope ul b patient enough to help me out..

1. I want to display the data that i enter in 4 text boxes.. in my datagridview
my datagridview has 4 columns name, quantity, price per unit and total

textbox1 = name
textbox2 = quantity
textbox3 = price per unit
textbox4 = total


2. i want to display the sum of the column "total" in a label text...


hoping to c some more simple n easy to use codes like u have provided me till now:nevreness:
 
Back
Top