I am trying to insert data into a table and then display that data in a DataGridView.
When I insert the data at runtime, the entered data displays in a DataGridView however after termination of the application, nothing is stored in my "mdf" file.
I am using piping method to store my connection string and setting the property "copy to output" of my mdf file to "copy always".
Does anybody know why my database is not retaining the data after terminating the application?
Code:
Dim con As New SqlConnection(System.Configuration.ConfigurationManager.ConnectionStrings("mycs").ConnectionString)
Dim cmd As New SqlCommand
cmd.Connection = con
con.Open()
cmd.CommandText = "insert into tbl_party_data (pname, paddress, ppan, pmobile, pemail) values ('" & txt1.Text & "','" & txt2.Text & "','" & txt3.Text & "','" & txt4.Text & "','" & txt5.Text & "')"
cmd.ExecuteNonQuery()
Dim adp As SqlDataAdapter = New SqlDataAdapter _
("select * from tbl_party_data", con)
Dim ds As DataSet = New DataSet()
adp.Fill(ds)
DataGridView1.DataSource = ds.Tables(0)
Related
I'm using datetimepicker to choose the date to show the data from the microsoft sql server but the cells are empty.
I use LIKE operator to show the purchased item within the day.
This is the code:
Public Sub see4()
'MYPOPUL4TE D4T4GRIDVIEW3
Dim query As String = "SELECT * FROM projectsd.dbo.cart WHERE dates LIKE '" & DateTimePicker1.Text & "'"
Using cmd As SqlCommand = New SqlCommand(query, con)
Using da As New SqlDataAdapter
da.SelectCommand = cmd
Using dt As New DataTable()
da.Fill(dt)
DataGridView3.DataSource = dt
End Using
End Using
End Using
End Sub
I'm trying to update datagridview after inserting a new record.
The following button is located on a form and the datagridview is located on another form:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Try
Dim dt As New DataTable
Using DBCon As New SqlConnection("Server=192.168.1.4,1433;Database=example;User=test;Pwd=test;"),
DBCmd As New SqlCommand("INSERT INTO Jobs (Zone,Equipement,Description)
VALUES ( '" & TextEdit1.Text & "','" & TextEdit2.Text & "','" & TextEdit3.Text & "') ", DBCon)
DBCon.Open()
dt.Load(DBCmd.ExecuteReader)
mainform.GridControl1.DataSource = dt
End Using
MsgBox.Show("Record Has been Added Successfully!!")
DialogResult = DialogResult.OK
Catch ex As Exception
MsgeBox.Show(ex.Message)
End Try
You need to write the below code. Request you to go through this link to know how to parametrize your query with CRUD demo.
dt.Load(DBCmd.ExecuteReader)
mainform.GridControl1.DataSource = dt
mainform.GridControl1.Refresh()
I'm trying to retrieve data from sql server to vb.net textbox but i don't know what else to do all the tutorials i have are just to retrieve records from the database to datagrid view..please help..
Private Sub txtrfid_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtrfid.KeyPress
cn.Open()
With cmd
.Connection = cn
.CommandText = "SELECT * FROM Students WHERE RFID like '%" & txtrfid.Text & "%'"
End With
MsgBox("Record Found!", MsgBoxStyle.Information, "Update")
da.SelectCommand = cmd
dt.Clear()
da.Fill(dt)
cn.Close()
txtname.Text = 'Firstname'
You're populating a DataTable with the data from the database so you then have to get the data from that DataTable into the TextBox. You can do that with data binding, which is how you've probably seen it done with a grid, e.g.
txtname.DataBindings.Add("Text", dt, "Firstname")
That's definitely how you'd do it if you were retrieving multiple records that you wanted to be able to navigate, although you'd probably use a BindingSource in between. If there's only one record then you might instead just move the data manually, e.g.
txtname.Text = CStr(dt.Rows(0)("Firstname"))
If you want to display only a single value (FirstName) from Table then see following piece of code
Using conn As New SqlConnection("connstr")
conn.Open()
Dim cmd As New SqlCommand("", conn)
Dim txtName As String
cmd.CommandText = "SELECT firstname FROM Students WHERE RFID ='" & txtrfid.Text & "'"
txtName = IIf(IsDBNull(cmd.ExecuteScalar), "", cmd.ExecuteScalar)
If txtName <> "" Then
MsgBox("Record Found!", MsgBoxStyle.Information, "Update")
Textbox1.Text = ""
Textbox1.Text = txtName
else
MsgBox("No Record Found!", MsgBoxStyle.Information, "INFO.")
End If
End Using
There are many ways to retrieve the data. You can simply retrieve the data from sql database to textbox using sql data reader which is one of my favourite. Let me share to you.
Note : Don't forget to import system.data.sqlclient
Private Sub txtrfid_KeyPress(ByVal sender As System.Object, ByVal e As System.Windows.Forms.KeyPressEventArgs) Handles txtrfid.KeyPress
strConn = "Data Source=" & servernamehere & ";Initial Catalog=" & databasenamehere & ";User ID=" & userid & ";Password=" & password
sqlConn = New SqlConnection(strConn)
sqlConn.Open()
Dim sqlcmd As New SqlCommand("Your query here", sqlConn)
Dim myreader As SqlDataReader
myreader = sqlcmd.ExecuteReader()
myreader.Read()
If myreader.HasRows Then
txtrfid.Text = myreader.Item("column name from sql database table").Tostring
End If
sqlConn.Close()
End Sub
You may catch the exception with Try-Catch Technique.
I'm trying to insert new data into an SQL server from what user had input in a textbox.
My code is not functioning, please help me.
Dim con As New SqlConnection
Dim cmd As New SqlCommand
con.ConnectionString = "Data Source=LANDSLIDE\SQLSERVER2005;Initial Catalog=PayrollSQL;User ID=sa;Password=lismyadmin"
cmd.Connection = con
con.Open()
cmd.CommandText = "INSERT INTO dbo.Member ([Name], [Code], [NRIC]) VALUES(" & TextBox1.Text & "," & TextBox2.Text & "," & TextBox3.Text & ")"
cmd.ExecuteNonQuery()
con.Close()
You ought to use parameterised queries. Something like:
cmd.CommandText = "INSERT INTO dbo.Member ([Name], [Code], [NRIC]) VALUES(#Name,#Code,#NRIC)"
cmd.Parameters.AddWithValue("#Name",TextBox1.Text)
cmd.Parameters.AddWithValue("#Code",TextBox2.Text)
cmd.Parameters.AddWithValue("#NRIC",TextBox3.Text)
At the moment, you're ending up with your strings appearing literally in your SQL statement - and whatever values you're retrieving from TextBox1-3 aren't valid pieces of SQL (hopefully - if they are actually valid SQL, you're performing SQL injection)
(By the way, you're allowed to rename controls - you ought to rename those TextBoxes so that you don't have to remember that, say, TextBox2 is the one for code)
May be it will work or else provide error message:
cmd.CommandText = "INSERT INTO dbo.Member ([Name], [Code], [NRIC]) VALUES('" & TextBox1.Text & "','" & TextBox2.Text & "','" & TextBox3.Text & "')"
I'm using this code to add a new column:
DataGridView1.Columns.Add("Test", "TesT")
but after saving and closing the project then I open it again the columns were deleted.
The question is how to save the columns to the access database.
If your just going to save the values, than you'd have to call them back up in order to fill the grid.
Sample code to insert into an access db.
Dim Cmd as New OLEDb.OleDbCommand
Dim oleConn as New OleDb.OleDbConnection
Dim strDataBase as String
strDataBase = "C:\Test.mdb"
'2007 connection string will work for 2003-2007 databases - change to 14.0 for 2010 etc
oleConn.ConnectionString = "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" & strDataBase & ";Jet OLEDB"
Cmd.Connection = oleConn
oleConn.Open()
Cmd.CommandText = "Insert into mytable ( Field1, Field2 ) values ( '" & "some Value" & "', '" & "some value" & "' )"
Cmd.ExcuteNonQuery
oleConn.Close()