hi my name is vishal For past 3 days i have been breaking my head on how to make hscrollbar control to navigate through records from database(sql server2008) in c# windows forms?
Given below is c# code of my form named:
frmReprocessor:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace DRRS_CSharp
{
public partial class frmReprocessor : Form
{
int pUserID;
private string conString = "Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true";
private DataSet dataset;
private string SelectString = ("Select * from Reprocess");
public frmReprocessor()
{
InitializeComponent();
}
private void btnCancel_Click(object sender, EventArgs e)
{
this.Close();
}
private void fnGetDatabaseConnection()
{
SqlConnection con = new SqlConnection(conString);
SqlDataAdapter dadapter = new SqlDataAdapter(SelectString, con);
dataset = new DataSet();
dadapter.Fill(dataset, "Reprocess");
}
private void fnGetDataBindings()
{
this.lblRecPos.Text = "Record : " + ((this.BindingContext[dataset, "Reprocess"].Position + 1).ToString());
this.txtMName.DataBindings.Add("Text", dataset, "Reprocess.ManufacturerName");
this.txtMSno.DataBindings.Add("Text", dataset, "Reprocess.MFR_SL_No");
this.cboVirology.DataBindings.Add("Text", dataset, "Reprocess.volume");
this.txtIPAddress2.DataBindings.Add("Text", dataset, "Reprocess.DCS_ip_address");
this.trackBar1.Maximum = this.BindingContext[dataset, "Reprocess"].Count-1;
}
private void btnNew_Click(object sender, EventArgs e)
{
if (btnNew.Text == "NEW")
{
txtIPAddress2.Visible = false;
txtMName.Enabled = true;
txtMSno.Enabled = true;
cboVirology.Enabled = true;
txtMName.Text = "";
txtMSno.Text = "";
cboVirology.SelectedIndex = -1;
txtip1.Visible = true;
txtip2.Visible = true;
txtip3.Visible = true;
txtip4.Visible = true;
btnNew.Text = "CREATE";
txtMName.Focus();
return;
}
string dFieldName = "";
Boolean vEmptyB = false;
if (txtMName.Text.ToString().Trim() == "")
{
vEmptyB = true;
dFieldName = "Please enter Manufacturer name";
}
else if (txtMSno.Text.ToString().Trim() == "")
{
vEmptyB = true;
dFieldName = "Please enter Manufacturer serial number";
}
else if (cboVirology.Text.ToString().Trim() == "")
{
vEmptyB = true;
dFieldName = "Please select state of dialyzer";
}
if (vEmptyB == true)
{
MessageBox.Show(dFieldName + "should not be empty");
return;
}
SqlConnection conn = new SqlConnection("Data Source=NPD-4\\SQLEXPRESS;Initial Catalog=DRRS;Integrated Security=true");
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
int autoGenId = -1;
SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandType = CommandType.Text;
cmd = new SqlCommand("Insert into Reprocess(ManufacturerName,MFR_SL_No,volume,row_upd_date,user_id,DCS_ip_address)" + "Values(@ManufacturerName,@MFR_SL_No,@volume,GetDate(),@user_id,@DCS_ip_address); Select @autoGenId=SCOPE_IDENTITY();", conn);
cmd.Parameters.AddWithValue("@ManufacturerName", txtMName.Text.ToString());
cmd.Parameters.AddWithValue("@MFR_SL_No", txtMSno.Text);
cmd.Parameters.AddWithValue("@volume", cboVirology.Text);
cmd.Parameters.AddWithValue("@user_id", pUserID);
cmd.Parameters.AddWithValue("@DCS_ip_address", txtip1.Text + "." + txtip2.Text + "." + txtip3.Text + "." + txtip4.Text);
cmd.Parameters.Add("@autoGenId", SqlDbType.Int).Direction = ParameterDirection.Output;
cmd.ExecuteNonQuery();
autoGenId = Convert.ToInt32(cmd.Parameters["@autoGenId"].Value);
((MDIParent1)this.MdiParent).updateUserActivities(autoGenId, 9, txtMName.Text.ToString() + "Reprocessor detail was added successfully");
MessageBox.Show("Reprocessor Detail was added successfully", "DRRS", MessageBoxButtons.OK, MessageBoxIcon.Information);
this.Close();
}
private void frmReprocessor_Load(object sender, EventArgs e)
{
fnGetDatabaseConnection();
fnGetDataBindings();
txtip1.Visible = false;
txtip2.Visible = false;
txtip3.Visible = false;
txtip4.Visible = false;
}
The above code works with no problem at all!
I have achieved navigation of records from database(sql server2008) in c# windows forms using Track bar control and used it successfully! However i have to make transition from
trackbar control to
hscrollbar control in c# windows forms with sql server2008 since it is my
BOSS orders. I have browsed through net trying to embed similar functionality to hscrollbar control with no success.
I tried a little coding in hscrollbar control in c# windows forms with sql server2008. Given below is the code:
private void hScrollBar1_Scroll(object sender, ScrollEventArgs e)
{
txtMName.Enabled = false;
txtMSno.Enabled = false;
cboVirology.Enabled = false;
txtIPAddress2.Enabled = false;
txtip1.Visible = false;
txtip2.Visible = false;
txtip3.Visible = false;
txtip4.Visible = false;
int recordNr;
recordNr = this.BindingContext[dataset, "Reprocess"].Position = hScrollBar1.Value;
++recordNr;
this.label10.Text = "Record : " + recordNr.ToString();
if ((this.BindingContext[dataset, "Reprocess"].Position) < (this.BindingContext[dataset, "Reprocess"].Count - 1))
{
this.BindingContext[dataset, "Reprocess"].Position += 1;
}
else
{
ShowMessageBoxService.fnShowMessageBoxwithParameters("You´ve reached the end of the records",
"LAST RECORD: " + recordNr.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Information, 0, 0);
}
if ((this.BindingContext[dataset, "Reprocess"].Position) > (this.BindingContext[dataset, "Reprocess"].Count + 1))
{
this.BindingContext[dataset, "Reprocess"].Position -= 1;
}
if (this.BindingContext[dataset, "Reprocess"].Position == 1)
ShowMessageBoxService.fnShowMessageBoxwithParameters("You´ve reached the beginning of the records",
"FIRST RECORD: " + recordNr.ToString(),
MessageBoxButtons.OK,
MessageBoxIcon.Information, 0, 0);
}
The above code only works partially! I did a research on
hscrollbar control and found out a code that worked perfectly according to what i exactly i wanted. But unfortunately that code was in
vb6 with Ms access using Adodb. Given below is
vb6 code of
hscrollbar control.:
Option Explicit
Dim myRec As New ADODB.Recordset
Private Sub scrRec_Change()
On Error GoTo errh
myRec.MoveFirst
myRec.Move scrRec.Value
lblRecPos.Caption = "Record Pos ( " & scrRec.Value + 1 & "/" & myRec.RecordCount & " )"
If (myRec.EOF = False) Then
txtMName.Enabled = False
txtMSno.Enabled = False
cboVirology.Enabled = False
txtMName.Text = myRec("ManufacturerName").Value
txtMSno.Text = myRec("MFR_SL_No").Value
cboVirology.Text = myRec("volume").Value
txtIPAddress.Text = myRec("DCS_ip_address").Value
myRec.MoveNext
End If
Exit Sub
errh:
MsgBox Err.Description, vbCritical
End Sub
where
lblRecPos is name of label control next to hscrollbar in
vb6 with Ms access using
Adodb.
Can anyone help me on how to achieve the same functionality as i achieved of hscrollbar control in
vb6 with Ms access using Adodb in
c# windows forms with sql server2008?
Can anybody help me please? Any help/guidance in solving of this problem would be greatly appreciated! Can anyone help me please!