Last active
July 18, 2016 19:13
-
-
Save arn-ob/60bd7a6bc0efd993511a47226712e874 to your computer and use it in GitHub Desktop.
Multiple item insert by using ListView at C# (Win Form Code)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.ComponentModel; | |
using System.Data; | |
using System.Data.SqlClient; | |
using System.Drawing; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
using System.Windows.Forms; | |
namespace DataBindTestAtTextBox | |
{ | |
public partial class Form1 : Form | |
{ | |
public Form1() | |
{ | |
InitializeComponent(); | |
} | |
private void button1_Click(object sender, EventArgs e) | |
{ | |
if (listView1.Items.Count == 0) | |
{ | |
ListViewItem lst = new ListViewItem(); | |
lst.SubItems.Add(textBox1.Text); | |
lst.SubItems.Add(textBox2.Text); | |
listView1.Items.Add(lst); | |
return; | |
} | |
ListViewItem lst1 = new ListViewItem(); | |
lst1.SubItems.Add(textBox1.Text); | |
lst1.SubItems.Add(textBox2.Text); | |
listView1.Items.Add(lst1); | |
return; | |
} | |
private void button2_Click(object sender, EventArgs e) | |
{ | |
SqlConnection sql = new SqlConnection("user id=arnob;" + | |
"password=01746H3llow?arn;server=DESKTOP-KM6DK73;" + | |
"Trusted_Connection=yes;" + | |
"database=test;"); | |
sql.Close(); | |
for (int i = 0; i <= listView1.Items.Count - 1; i++) | |
{ | |
SqlCommand cmd = new SqlCommand(); | |
cmd.CommandText = "insert into dbo.ListViewCheck(Name, PhoneNo) VALUES (@Name,@PhoneNo)"; | |
cmd.CommandType = CommandType.Text; | |
cmd.Connection = sql; | |
cmd.Parameters.AddWithValue("Name", listView1.Items[i].SubItems[1].Text); | |
cmd.Parameters.AddWithValue("PhoneNo", listView1.Items[i].SubItems[2].Text); | |
sql.Open(); | |
cmd.ExecuteNonQuery(); | |
sql.Close(); | |
} | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment