Hi,
I have one table and two tools:
“TblCompany”
“txtAccountNumber”
“btnRun”
My table looks like this:
ID | Account | Name
1 5.6 Selling
2 3.5 Bying
3 1.4 Mobile
I wrote some codes that make a normal and simple filter. The target filter is one the column “Account”.
Then why I’am doing to resolv it? I put each ID number inside of my codes and copy/paste the same block of codes to filter the next ID number to make the operation. But there is a problem? This table is growing up every day and its becoming little bit havy when processing. Then how can I do to make a one way loop until the latest record of my table displaying one by one the next account’s number that will be saved later and so on.
Please see my codes:
I have one table and two tools:
“TblCompany”
“txtAccountNumber”
“btnRun”
My table looks like this:
ID | Account | Name
1 5.6 Selling
2 3.5 Bying
3 1.4 Mobile
I wrote some codes that make a normal and simple filter. The target filter is one the column “Account”.
Then why I’am doing to resolv it? I put each ID number inside of my codes and copy/paste the same block of codes to filter the next ID number to make the operation. But there is a problem? This table is growing up every day and its becoming little bit havy when processing. Then how can I do to make a one way loop until the latest record of my table displaying one by one the next account’s number that will be saved later and so on.
Please see my codes:
C#:
//Filtering Accounting Number: 5.6
string connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string commandString = "select top 1 * from tblTest1 where ID='" + “5.6” + "' order by ID DESC";
SqlCommand sqlCmd = new SqlCommand(commandString, conn);
SqlDataReader read = sqlCmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
txtAccount.Text = read["Account"].ToString();
txtName.Text = read["Name"].ToString();
}
}
else
{
}
read.Close();
conn.Close();
conn = new SqlConnection"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";);
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlParameter account = new SqlParameter("@account", SqlDbType.Varchar);
SqlParameter name = new SqlParameter("@name", SqlDbType.Varchar);
comm.Parameters.Add(account);
comm.Parameters.Add(name);
account.Value = txtAccount.Text;
name.Value = txtName.Text;
comm.Connection = conn;
comm.CommandText = "insert into tblTest2 ([account],[name])values(@account,@name)";
{
{
try
{
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
//Filtering Account number: 3.5
string connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string commandString = "select top 1 * from tblTest1 where ID='" + “3.5” + "' order by ID DESC";
SqlCommand sqlCmd = new SqlCommand(commandString, conn);
SqlDataReader read = sqlCmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
txtAccount.Text = read["Account"].ToString();
txtName.Text = read["Name"].ToString();
}
}
else
{
}
read.Close();
conn.Close();
conn = new SqlConnection"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";);
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlParameter account = new SqlParameter("@account", SqlDbType.Varchar);
SqlParameter name = new SqlParameter("@name", SqlDbType.Varchar);
comm.Parameters.Add(account);
comm.Parameters.Add(name);
account.Value = txtAccount.Text;
name.Value = txtName.Text;
comm.Connection = conn;
comm.CommandText = "insert into tblTest2 ([account],[name])values(@account,@name)";
{
{
try
{
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
//Filtering 1.4
string connectionString = "Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";
SqlConnection conn = new SqlConnection(connectionString);
conn.Open();
string commandString = "select top 1 * from tblTest1 where ID='" + “1.4” + "' order by ID DESC";
SqlCommand sqlCmd = new SqlCommand(commandString, conn);
SqlDataReader read = sqlCmd.ExecuteReader();
if (read.HasRows)
{
while (read.Read())
{
txtAccount.Text = read["Account"].ToString();
txtName.Text = read["Name"].ToString();
}
}
else
{
}
read.Close();
conn.Close();
conn = new SqlConnection"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Flex\\Accounting.mdf;Integrated Security = True";);
if (conn.State != ConnectionState.Open)
conn.Open();
SqlCommand comm = new SqlCommand();
comm.Connection = conn;
SqlParameter account = new SqlParameter("@account", SqlDbType.Varchar);
SqlParameter name = new SqlParameter("@name", SqlDbType.Varchar);
comm.Parameters.Add(account);
comm.Parameters.Add(name);
account.Value = txtAccount.Text;
name.Value = txtName.Text;
comm.Connection = conn;
comm.CommandText = "insert into tblTest2 ([account],[name])values(@account,@name)";
{
{
try
{
comm.ExecuteNonQuery();
}
finally
{
conn.Close();
}
Last edited: