TECHNOLOGIES
FORUMS
JOBS
BOOKS
EVENTS
INTERVIEWS
Live
MORE
LEARN
Training
CAREER
MEMBERS
VIDEOS
NEWS
BLOGS
Sign Up
Login
No unread comment.
View All Comments
No unread message.
View All Messages
No unread notification.
View All Notifications
Answers
Post
An Article
A Blog
A News
A Video
An EBook
An Interview Question
Ask Question
Forums
Monthly Leaders
Forum guidelines
Israel
698
1.3k
217.6k
How to make a simple loop until last record
Jan 15 2020 10:56 PM
Hi,
I would like to do a Loop with these codes. But I need to explain very well what I try to do.
1) On my form I have a Datagrid, one TextBox and a Button
2) I have a table name “Test”with a 2 columns (Company_name and Company). This table have 5 records
3) After displaying each “Company” its should send all company’s names into one table named “Receive”
Let’s see what I do. Its works but without with loop:
Company_name Company
--------------------- ------------
Andrews Company1
McSean Company2
Nokia Company2
TelCell Company1
Flex Company1
conn.Open();
SqlCommand cmd1 = conn.CreateCommand();
cmd1.CommandType = CommandType.Text;
cmd1.CommandText = "select * from Test where company = @company;
var param1 =
new
SqlParameter(
"@company"
, SqlDbType.NChar);
param1.Value = txtBoxCompany.Text;
{
cmd1.Parameters.Add(param1);
cmd1.Parameters.Add(param2);
}
cmd1.ExecuteNonQuery();
DataTable dt1 =
new
DataTable();
SqlDataAdapter da1 =
new
SqlDataAdapter(cmd1);
da1.Fill(dt1);
conn.Close();
dgvTest.DataSource = dt1;
Then after displaying the result on my datagrid its should send
in
block names of company on other table “Receive”
string
myConnectionString;
myConnectionString =
"Data Source = (LocalDB)\\MSSQLLocalDB; AttachDbFilename=C:\\Test\\WindowsFormsApplication1\\App_datas\\Accounting.mdf;Integrated Security = True;Integrated Security = True"
;
using
(var con =
new
SqlConnection())
{
con.ConnectionString = myConnectionString;
con.Open();
using
(var cmd =
new
SqlCommand())
{
cmd.Connection = con;
cmd.CommandType = System.Data.CommandType.Text;
cmd.CommandText = @
"INSERT INTO Test (company_name, company) SELECT company_name, company FROM test where company = @company"
;
cmd.Parameters.AddWithValue(
"@company"
, txtBox_company.Text);
cmd.ExecuteNonQuery();
}
con.Close();
}
Console.WriteLine(
"Done."
);
But all of these should doing as a loop one by one record until read and transfering the last record. Many thanx.
Reply
Answers (
2
)
C# code to me made better with Lambda expressions
How to genarate a bar code in asp.net web application