Israel

Israel

  • 699
  • 1.3k
  • 217.5k

My search Button doesnt work...

Jan 27 2014 6:42 AM
Hi!
 
I have a problem. All these code work but I would like to make a search in my database showing the record found on my textbox. But when I click on the search button. There is aways this message "input incorrect" or "record not found".
What happen? where do I the mistake in my code.
 
 
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace WindowsFormsApplication1
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
DataRowView drv;
System.Data.SqlServerCe.SqlCeConnection con;
System.Data.SqlServerCe.SqlCeDataAdapter da;
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
DataSet ds1;
int MaxRows = 0;
int inc = 0;
private void Form1_Load(object sender, EventArgs e)
{
// TODO: This line of code loads data into the 'employeesDataSet.tbl_employees' table. You can move, or remove it, as needed.
//this.tbl_employeesTableAdapter.Fill(this.employeesDataSet.tbl_employees);
con = new System.Data.SqlServerCe.SqlCeConnection();
con.ConnectionString = "DataSource=c:\\Users\\INAC\\Documents\\Employees.sdf";
Instance=True";
con.Open();
ds1 = new DataSet();
string sql = "SELECT * From tbl_employees";
da = new System.Data.SqlServerCe.SqlCeDataAdapter(sql, con);
da.Fill(ds1, "Workers");
NavigateRecords();
//MessageBox.Show("Open");
con.Close();
}
private void NavigateRecords()
{
DataRow dRow = ds1.Tables["Workers"].Rows[inc];
textBox1.Text = dRow.ItemArray.GetValue(1).ToString();
textBox2.Text = dRow.ItemArray.GetValue(2).ToString();
textBox3.Text = dRow.ItemArray.GetValue(3).ToString();
textBox4.Text = dRow.ItemArray.GetValue(4).ToString();
MaxRows = ds1.Tables["Workers"].Rows.Count;
}
private void btnNext_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc++;
NavigateRecords();
}
else
{
MessageBox.Show("No more rows");
}
}
private void btnPrevious_Click(object sender, EventArgs e)
{
if (inc > 0)
{
inc--;
NavigateRecords();
}
else
{
MessageBox.Show("First Record");
}
}
private void btnLast_Click(object sender, EventArgs e)
{
if (inc != MaxRows - 1)
{
inc = MaxRows - 1;
NavigateRecords();
}
else
{
MessageBox.Show("Last Record");
}
}
private void btnFirst_Click(object sender, EventArgs e)
{
if (inc != 0)
{
inc = 0;
NavigateRecords();
}
}
private void btnAddNew_Click(object sender, EventArgs e)
{
textBox1.Clear();
textBox2.Clear();
textBox3.Clear();
textBox4.Clear();
}
private void btnSave_Click(object sender, EventArgs e)
{
DataRow dRow = ds1.Tables["Workers"].NewRow();
dRow[1] = textBox1.Text;
dRow[2] = textBox2.Text;
dRow[3] = textBox3.Text;
dRow[4] = textBox4.Text;
ds1.Tables["Workers"].Rows.Add(dRow);
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da);
cb.DataAdapter.Update(ds1.Tables["Workers"]);
MaxRows = MaxRows + 1;
inc = MaxRows - 1;
btnAddNew.Enabled = true;
btnSave.Enabled = false;
}
private void UpdateDB()
{
System.Data.SqlServerCe.SqlCeCommandBuilder cb;
cb = new System.Data.SqlServerCe.SqlCeCommandBuilder(da);
cb.DataAdapter.Update(ds1.Tables["Workers"]);
}
private void btnUpdate_Click(object sender, EventArgs e)
{
DataRow dRow2 = ds1.Tables["Workers"].Rows[inc];
dRow2[1] = textBox1.Text;
dRow2[2] = textBox2.Text;
dRow2[3] = textBox3.Text;
dRow2[4] = textBox4.Text;
UpdateDB();
MessageBox.Show("Data Update");
}
private void btnDelete_Click(object sender, EventArgs e)
{
ds1.Tables["Workers"].Rows[inc].Delete();
UpdateDB();
MaxRows = ds1.Tables["Workers"].Rows.Count;
inc--;
NavigateRecords();
MessageBox.Show("Record Deleted");
}
//The mistake it's here!!! 
private void btnSearch_Click(object sender, EventArgs e)
{
try
{
DataView dview = new DataView(ds1.Tables[0]);
dview.Sort = "first_name";
int i = dview.Find(textBox5.Text);
if (i < 0)
{
MessageBox.Show("No Record Found");
}
else
{
drv = (DataRowView)cmgr.Current;
textBox1.Text = drv.Row[0].ToString();
textBox2.Text = drv.Row[1].ToString();
textBox3.Text = drv.Row[2].ToString();
textBox4.Text = drv.Row[3].ToString();
}
}
catch (Exception)
{
MessageBox.Show("Incorrect Input..");
textBox5.Clear();
textBox5.Focus();
}
}
}
}
 
 

Answers (1)