VB.NET - Navigate

Asked By Bigboy
11-Feb-12 07:07 AM
How can i navigate from one record to another (the data appearing in textboxes) for database stored information in vb 2010.
  dipa ahuja replied to Bigboy
11-Feb-12 07:59 AM
public int cnt;
OleDbDataAdapter da;
DataSet ds;
private void testing_Load(object sender, EventArgs e)
{
  da = new OleDbDataAdapter("Select * from emp", "ConnectionString");     
  ds = new DataSet();
  da.Fill(ds, "emp");        
  showdata(0);
}
public void showdata(int i)
{
  txtid.Text = ds.Tables[0].Rows[i][0].ToString();
  txtname.Text = ds.Tables[0].Rows[i][1].ToString();
}
private void btnmovefirst_Click(object sender, EventArgs e)
{
  BindingContext[ds.Tables[0]].Position = 0;
  cnt = BindingContext[ds.Tables[0]].Position;
  showdata(0);
       
}
 
private void btnmovePrevious_Click(object sender, EventArgs e)
{
  int c = Convert.ToInt32(ds.Tables[0].Rows.Count.ToString());
      
  if ((BindingContext[ds.Tables[0]].Position) == 0)
  {
    MessageBox.Show("Begin of Records Reached!!");          
 
  }
  else
  {
    int i = BindingContext[ds.Tables[0]].Position -= 1;
    BindingContext[ds.Tables[0]].Position = i;
    showdata(i);        
  }
}
 
private void btnmoveNext_Click(object sender, EventArgs e)
{
  int c = Convert.ToInt32(ds.Tables[0].Rows.Count.ToString());
                  
  if ((BindingContext[ds.Tables[0]].Position) == c - 1)
  {
    MessageBox.Show("End of Records Reached!!");                
  }
  else
  {
    int i = BindingContext[ds.Tables[0]].Position += 1;
    BindingContext[ds.Tables[0]].Position = i;
    showdata(i);
  }
}
 
private void btnmoveLast_Click(object sender, EventArgs e)
{
  int c = Convert.ToInt32(ds.Tables[0].Rows.Count.ToString());
      
  BindingContext[ds.Tables[0]].Position = c-1;
  showdata(c - 1);
      
}
 
  kalpana aparnathi replied to Bigboy
11-Feb-12 12:45 PM
hi,
Dim varConnection As New SqlConnection
Dim varDataSet As New DataSet
Dim varAdapter As SqlDataAdapter
Dim varCommand As SqlCommand
Dim varchar As SqlDataAdapter
 
Dim sql As String
 
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
Try
varConnection.Open()
varAdapter.Fill(varDataSet, "tbl_rop")
Dim rowRop1 As System.Data.DataRow
 
Catch ex As Exception
 
End Try
 
sql = "SELECT * FROM tbl_rop"
 
sql = "SELECT distinct * FROM tbl_rop"
'Dim con As String = ConfigurationSettings.AppSettings("preeconn")
varCommand = New SqlCommand(varQuery, varConnection)
 
varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)
'Dim maxrws As String
Dim i As Integer
varAdapter.Fill(varDataSet)
 
'sql = varAdapter.Fill(varDataSet)Rows.Count.
'varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
i = 0
For i = 0 To -1
TextBox1.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Name_User")
TextBox2.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Date_of_birth")
 
Next i
TextBox1.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Name_user")
TextBox2.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Date_of_birth")
 i = 0
For i = 0 To -1
TextBox1.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Name_User")
TextBox2.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Date_of_birth")
TextBox3.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Hobbies")
TextBox4.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Phone_number")
 
Next i
TextBox1.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Name_user")
TextBox2.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Date_of_birth")
TextBox3.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Hobbies")
TextBox4.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Phone_number")
 
 

 
TextBox2.Text = varDataSet.Tables("tbl_rop").Rows(0).Item("Date_of_birth")
 
 
this the code for next button..
 
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim varConnection As New SqlConnection
Dim varDataSet As New DataSet
Dim varAdapter As SqlDataAdapter
Dim varCommand As SqlCommand
Dim varchar As SqlDataAdapter
Dim Record_num = 0
Dim sql As String
 
 
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
 
sql = "SELECT * FROM tbl_rop"
 
sql = "SELECT distinct * FROM tbl_rop"
'Dim con As String = ConfigurationSettings.AppSettings("preeconn")
varCommand = New SqlCommand(varQuery, varConnection)
 
varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)
 
 
 
 
 
varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
Record_num = +1
TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
 
 
End Sub
 

  If Record_num <> Number_of_Rows - 1 Then
  Record_num = +1
  TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
  TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
   

Public Class form1
Dim Record_num As Integer = 0
End Class
 
 
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim varConnection As New SqlConnection
Dim varDataSet As New DataSet
Dim varAdapter As SqlDataAdapter
Dim varCommand As SqlCommand
Dim varchar As SqlDataAdapter
Dim sql As String
Dim Record_num = 0
Dim Number_of_Rows As Integer = 0
 
 
 
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
 
sql = "SELECT * FROM tbl_rop"
 
sql = "SELECT distinct * FROM tbl_rop"
 
varCommand = New SqlCommand(varQuery, varConnection)
 
varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)
 
 
varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
If Record_num <> Number_of_Rows - 1 Then
Record_num = +1
TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
TextBox3.Text = varDataSet.Tables(0).Rows(Record_num).Item("Hobbies")
TextBox4.Text = varDataSet.Tables(0).Rows(Record_num).Item("Phone_number")
 
End If
 
End Sub
    Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
  Dim varConnection As New SqlConnection
  Dim varDataSet As New DataSet
  Dim varAdapter As SqlDataAdapter
  Dim varCommand As SqlCommand
  Dim varchar As SqlDataAdapter
  Dim sql As String
  Dim Record_num = 0 ' you do not need this here
  Dim Number_of_Rows As Integer = 0 'why zero? this will hold number of rows in your table so you will be sure that once you reached last record you will not increment it
  varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
 
  sql = "SELECT * FROM tbl_rop"
 
  sql = "SELECT distinct * FROM tbl_rop"
 
  varCommand = New SqlCommand(varQuery, varConnection)
 
  varCommand = New SqlCommand(sql, varConnection)
  varAdapter = New SqlDataAdapter(varCommand)
 
 
 
 
 
  varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
 
  Number_of_Rows=varDataSet .Tables("your_table_name").Rows.Count
 
  If Record_num <> Number_of_Rows - 1 Then
  Record_num = +1
  TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
  TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
  End If
 
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim varConnection As New SqlConnection
Dim varDataSet As New DataSet
Dim varAdapter As SqlDataAdapter
Dim varCommand As SqlCommand
 
Dim sql As String
Dim Record_num = 0'i cannot remove the declaration for record num
Dim Number_of_Rows As Integer =8
 
 
 
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
 
sql = "SELECT * FROM tbl_rop"
 
sql = "SELECT distinct * FROM tbl_rop"
 
varCommand = New SqlCommand(varQuery, varConnection)
 
varCommand = New SqlCommand(sql, varConnection)
varAdapter = New SqlDataAdapter(varCommand)
 
 
varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
 
Number_of_Rows = varDataSet.Tables("tbl_rop").Rows.Count
 
If Record_num <> Number_of_Rows - 1 Then
Record_num = +1
TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
 
End If
 
End Sub
 
 
 
Private Sub btnNext_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNext.Click
Dim varConnection As New SqlConnection
Dim varDataSet As New DataSet
Dim varAdapter As SqlDataAdapter
Dim varCommand As SqlCommand
 
Dim sql As String
Dim Record_num = 0
Dim Number_of_Rows As Integer = 8
 
 
 
varConnection = New SqlConnection("Server=(local);user id=sa;password=;Initial Catalog=ROP1")
 
 
 
varAdapter.Fill(varDataSet) 'used to get the count of number of rows in a table
 
Number_of_Rows = varDataSet.Tables("tbl_rop").Rows.Count
 
If Record_num <> Number_of_Rows - 1 Then
Record_num = +1
TextBox1.Text = varDataSet.Tables(0).Rows(Record_num).Item("Name_User")
TextBox2.Text = varDataSet.Tables(0).Rows(Record_num).Item("Date_of_birth")
 
End If
End Sub
  Somesh Yadav replied to Bigboy
12-Feb-12 03:49 AM
Hi refer to the below link i hope it would be helpfull to you.

http://www.vb6.us/tutorials/database-access-vb6-data-control-tutorial
Create New Account
help
In the following code snippet i am using NorthWind MSAccess database. [CODE] strConnection = System.Configuration.ConfigurationSettings.AppSettings["NWConnection"]; OleDbConnection oConction = new OleDbConnection(strConnection); oConction.Open(); string SQL = "SELECT EmployeeID, TitleofCourtesy + ' ' + LastName + ', ' + FirstName as EmpName FROM Employees"; OleDbDataAdapter oDA = new OleDbDataAdapter(SQL, oConction); DataSet oDS = new DataSet(); oDA.Fill(oDS); DataView oView = new DataView(); oView = oDS CONN = new SqlConnection("<Connection String> "); SqlCommand selectCMD = new SqlCommand("<Selection Query", CONN ); selectCMD.CommandTimeout = 30; SqlDataAdapter DAEmp = new SqlDataAdapter(); DAEmp.SelectCommand = selectCMD; CONN.Open(); DataSet DSEmp = new DataSet(); DAEmp.Fill(DSEmp, "Employee"); listBox1.DataSource CONN = new SqlConnection("<Connection String> "); SqlCommand selectCMD = new SqlCommand("<Selection Query", CONN ); selectCMD.CommandTimeout = 30; SqlDataAdapter DAEmp = new SqlDataAdapter(); DAEmp.SelectCommand = selectCMD; CONN.Open(); DataSet DSEmp = new DataSet(); DAEmp.Fill(DSEmp, "Employee"); listBox1.DataSource
remotely, I get this error. Any reason for this? SqlConnection sqlConn = new SqlConnection(System.Configuration.ConfigurationSettings.AppSettings["ConnString"]); DataTable dt = new DataTable(); SqlDataAdapter objAdapter; string sql = "SELECT * FROM tblNgravisETDContact"; sqlConn.Open(); objAdapter = new SqlDataAdapter(sql, sqlConn); objAdapter.Fill(dt); DataRow objRow = dt.NewRow(); objRow["ETDID"] = base.GetNewSQLPKID("tblNgravisETDContact", "ETDID OleDbConnection(strConn); string strSQL = "SELECT CustomerID, CompanyName FROM Customers"; OleDbCommand cmd = new OleDbCommand(strSQL, cn); OleDbDataAdapter da = new OleDbDataAdapter(); da.SelectCommand = cmd; Note that a Command Object is created, and assigned to the DataAdapter
DataGrid DataGrid1; protected System.Web.UI.WebControls.TextBox TextBox1; public DataSet ds = new DataSet(); public OleDbDataAdapter odap; public OleDbConnection con; private void Page_Load( object sender, System.EventArgs e) { / / Put user code OleDbConnection(@"Provider = Microsoft.JET.OLEDB.4.0;data source = c: \ robert \ emp.mdb"); odap = new OleDbDataAdapter("select * from emp", con); odap.Fill(ds, "emp"); DataGrid1.DataSource = ds; DataGrid1.DataBind(); Label1.Text OleDbConnection(@"Provider = Microsoft.JET.OLEDB.4.0;data source = c: \ robert \ emp.mdb"); odap = new OleDbDataAdapter("select * from emp", con); odap.Fill(ds, "emp"); DataGrid1.DataSource = ds; DataGrid1.DataBind(); } } } This part object sender, EventArgs e) { if (!Page.IsPostBack) { BindTheData(); } } public void BindTheData() { string strConn = null; strConn = ConfigurationSettings.AppSettings("NorthwindConnection"); strSql = strSql + lblOrderBy.Text; DataSet myDataSet = new DataSet(); SqlDataAdapter myDataAdapter = new SqlDataAdapter(strSql, strConn); myDataAdapter.Fill(myDataSet, "Customers"); dtgCusts.Datasource = myDataSet.Tables["Customers"]; dtgCusts.DataBind(); } public void Page.IsPostBack Then BindTheData() End If End Sub Sub BindTheData() Dim strConn As String StrConn = ConfigurationSettings.AppSettings( "NorthwindConnection" ) strSql = strSql + lblOrderBy.Text Dim myDataSet As DataSet = new DataSet() Dim myDataAdapter As SqlDataAdapter = new SqlDataAdapter(strSql, strConn) myDataAdapter.Fill(myDataSet, "Customers" ) dtgCusts.Datasource = myDataSet.Tables( "Customers" ) dtgCusts.DataBind
binddata() { con = new SqlConnection ( "Data Source = MCN101; Initial Catalog = MergeTable; Uid = sa; pwd = " ); da = new SqlDataAdapter ( "Select * from Table1" , con); da.Fill(ds1, "Record" ); da = new SqlDataAdapter ( "select * from Table2" , con); da.Fill(ds2, "Record" ); ds1.Merge(ds2); GridAllRecord.DataSource = ds1; GridAllRecord as New SqlConnection (strConn) Dim DS1 As DataSet Dim DS1 As DataSet Dim MyCommand As SqlDataAdapter MyCommand = new SqlDataAdapter("exec s_get_table1", MyConnection) DS1 = new DataSet() MyCommand.Fill(DS1, "Table1") MyCommand = new SqlDataAdapter("exec s_get_table2", MyConnection) DS2 = new DataSet() MyCommand.Fill(DS2, "Table2") 'This code won't work the primary key. . . . . . . . . . . . . . . . . . . . . . string connectionString = "Enter your connection string here"; SqlConnection sqlConnection = new SqlConnection(connectionString); SqlDataAdapter sqlAdapter1 = new SqlDataAdapter("select * from Table1", sqlConnection); SqlDataAdapter sqlAdapter2 = new SqlDataAdapter("select * from Table2", sqlConnection); DataSet dataSet1 = new DataSet(); DataSet dataSet2 = new DataSet