Records do not show
-
Hello, I would like to ask why do I cannot display my records in my datagriview, the database I use is sql server 2005 with ".mdf" extension. Also if I use the wizard to add datasource I can add,delete records but when I run change the startup form to show the records via code connection the records I think is gone or something in my code is really wrong :confused: The code I use is here:
Imports System.Data.SqlClient
Public Class Form2Dim con As New SqlConnection Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_ "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_ "Integrated Security=True;User Instance=True" con.Open() con.Close() Dim da As New SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ds.Tables.Add(dt) con.Open() da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con) da.Fill(dt) con.Close() DataGridView1.DataSource = dt.DefaultView Catch ex As Exception MsgBox(ex.ToString) con.Close() End Try End Sub
End Class
Thanks, Dan
-
Hello, I would like to ask why do I cannot display my records in my datagriview, the database I use is sql server 2005 with ".mdf" extension. Also if I use the wizard to add datasource I can add,delete records but when I run change the startup form to show the records via code connection the records I think is gone or something in my code is really wrong :confused: The code I use is here:
Imports System.Data.SqlClient
Public Class Form2Dim con As New SqlConnection Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_ "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_ "Integrated Security=True;User Instance=True" con.Open() con.Close() Dim da As New SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ds.Tables.Add(dt) con.Open() da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con) da.Fill(dt) con.Close() DataGridView1.DataSource = dt.DefaultView Catch ex As Exception MsgBox(ex.ToString) con.Close() End Try End Sub
End Class
Thanks, Dan
Try using following code instead of yours.
void FillData() { // 1 // Open connection using (SqlConnection c = new SqlConnection(DataConnectionString)) { c.Open(); // 2 // Create new DataAdapter using (SqlDataAdapter a = new SqlDataAdapter("SELECT \* FROM phoneBookTable", c)) { // 3 // Use DataAdapter to fill DataTable DataTable t = new DataTable(); a.Fill(t); // 4 // Render data onto the screen // dataGridView1.DataSource = t; // <-- From your designer } } }
HTH
Jinal Desai - LIVE Experience is mother of sage....
-
Try using following code instead of yours.
void FillData() { // 1 // Open connection using (SqlConnection c = new SqlConnection(DataConnectionString)) { c.Open(); // 2 // Create new DataAdapter using (SqlDataAdapter a = new SqlDataAdapter("SELECT \* FROM phoneBookTable", c)) { // 3 // Use DataAdapter to fill DataTable DataTable t = new DataTable(); a.Fill(t); // 4 // Render data onto the screen // dataGridView1.DataSource = t; // <-- From your designer } } }
HTH
Jinal Desai - LIVE Experience is mother of sage....
Hello, Thanks for the help, but sorry to say I'm a bit confused about the code you have sent :( The code seems not working ing vb 2008 or maybe I am wrong :confused: Thanks over all in trying to help me. Thanks, Dan
-
Hello, Thanks for the help, but sorry to say I'm a bit confused about the code you have sent :( The code seems not working ing vb 2008 or maybe I am wrong :confused: Thanks over all in trying to help me. Thanks, Dan
-
The code I have posted is in CSharp. Just use the idea, you do not need to follow the exact code. Just follow the steps in VB, in your way. So, that you will get if you are missing something. HTH
Jinal Desai - LIVE Experience is mother of sage....
Hello, Thank, I try to study it :) I have some additional question's if you dont mind. I notice that when I edit the database record and add data on it, it can show the data that I have add via editing but when I add data into the database using the wizard or form the records seems to be dissapear everytime I change the startup form. (info: form1 - the form that I use to connect via wizard form2 - form I use via coding connection ) I'm so confused, I can add data but when everytime I change startup form the records I have added seems gone :confused: , also everytime I add new records it just overwrite and it doesn't add new row :( Below here is my code again :
Imports System.Data.SqlClient
Public Class Form2Dim con As New SqlConnection Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_ "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_ "Integrated Security=True;User Instance=True" con.Open() con.Close() Dim da As New SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ds.Tables.Add(dt) con.Open() da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con) da.Fill(dt) con.Close() Dim newRows As DataRow = dt.NewRow With newRows .Item(0) = "2" End With dt.Rows.Add(newRows) con.Open() Dim cb As New SqlCommandBuilder(da) da.Update(dt) con.Close() Dim newRows2 As DataRow = dt.NewRow With newRows2 .Item(0) = "3" End With dt.Rows.Add(newRows2) con.Open() cb = New SqlCommandBuilder(da) da.Update(dt) con.Close() DataGridView1.DataSource = dt.DefaultView Catch ex As Exception MsgBox(ex.ToString) con.Close() End Try End Sub
End Class
thanks again, Dan
-
Hello, Thank, I try to study it :) I have some additional question's if you dont mind. I notice that when I edit the database record and add data on it, it can show the data that I have add via editing but when I add data into the database using the wizard or form the records seems to be dissapear everytime I change the startup form. (info: form1 - the form that I use to connect via wizard form2 - form I use via coding connection ) I'm so confused, I can add data but when everytime I change startup form the records I have added seems gone :confused: , also everytime I add new records it just overwrite and it doesn't add new row :( Below here is my code again :
Imports System.Data.SqlClient
Public Class Form2Dim con As New SqlConnection Private Sub Form2\_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load Try con.ConnectionString = "Data Source=.\\SQLEXPRESS;" + \_ "AttachDbFilename=|DataDirectory|\\Database1.mdf;" + \_ "Integrated Security=True;User Instance=True" con.Open() con.Close() Dim da As New SqlDataAdapter Dim dt As New DataTable Dim ds As New DataSet ds.Tables.Add(dt) con.Open() da = New SqlDataAdapter("SELECT \* FROM phoneBookTable", con) da.Fill(dt) con.Close() Dim newRows As DataRow = dt.NewRow With newRows .Item(0) = "2" End With dt.Rows.Add(newRows) con.Open() Dim cb As New SqlCommandBuilder(da) da.Update(dt) con.Close() Dim newRows2 As DataRow = dt.NewRow With newRows2 .Item(0) = "3" End With dt.Rows.Add(newRows2) con.Open() cb = New SqlCommandBuilder(da) da.Update(dt) con.Close() DataGridView1.DataSource = dt.DefaultView Catch ex As Exception MsgBox(ex.ToString) con.Close() End Try End Sub
End Class
thanks again, Dan
-
I have found very nice link to perform, add, update and delete operations along with code. Add, Edit, and Delete in DataGridView with Paging[^] HTH
Jinal Desai - LIVE Experience is mother of sage....
Hello, Thanks for the nice link. This is really helpfull. Thanks, Dan
-
Hello, Thanks for the nice link. This is really helpfull. Thanks, Dan
-
Hello, A 5 would be good to all of you helping newbie programmers Thanks, Dan
-
Hello, A 5 would be good to all of you helping newbie programmers Thanks, Dan