Click here to Skip to main content
15,117,318 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more: , +
Dear All,


Here i working with windows application using visual studio 2008 and sql management studio express. I have one listview and that one having 3 columns and some data below that columns. But i dont know how to done this one.. All replies welcome....


Thanks in Advance

Dhinesh Kumar.V

I tried this one

C#
for (int i = 0; i < listView1.Items[i].Checked; i++)
           {
           SqlConnection _sqlconnection = new SqlConnection();
           string[] conString1 = DBSettings.connectionStringValues();
           string connectionString1 = string.Format(@"server=" + conString1[0] + ";database=POSNET_ROOM;uid=" + conString1[2] + ";pwd=" + conString1[3]);
           _sqlconnection.ConnectionString = connectionString1;
           _sqlconnection.Open();

          // SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);
         //  SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('" + listView1.Items[1].SubItems[0].Text + "','" + listView1.Items[1].SubItems[1].Text + "','" + listView1.Items[1].SubItems[2].Text + "','" + listView1.Items[1].SubItems[3].Text + "')", _sqlconnection);
           SqlDataAdapter _sqldataadapter = new SqlDataAdapter("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);

              // SqlDataReader _sqldatareader = _sqlcommand.ExecuteReader();
           _sqldatareader.Read();

           }
Posted
Updated 17-Aug-12 12:28pm
v2
Comments
Prabhakaran Soundarapandian 17-Aug-12 6:45am
   
What is question...can you improve your question so that we can help you????
Dhinesh kumar.V 17-Aug-12 7:11am
   
sure sir. I have one listview. and that one is having 3 columns like NAME, AGE, CLASS. i insert 3 data(name,age,class) at listview. But i dont know how to save this one database. because normal sql insert commond is not suitable for this one.

I tried this one

for (int i = 0; i < listView1.Items[i].Checked; i++)
{
SqlConnection _sqlconnection = new SqlConnection();
string[] conString1 = DBSettings.connectionStringValues();
string connectionString1 = string.Format(@"server=" + conString1[0] + ";database=POSNET_ROOM;uid=" + conString1[2] + ";pwd=" + conString1[3]);
_sqlconnection.ConnectionString = connectionString1;
_sqlconnection.Open();

// SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);
// SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('" + listView1.Items[1].SubItems[0].Text + "','" + listView1.Items[1].SubItems[1].Text + "','" + listView1.Items[1].SubItems[2].Text + "','" + listView1.Items[1].SubItems[3].Text + "')", _sqlconnection);
SqlDataAdapter _sqldataadapter = new SqlDataAdapter("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);

// SqlDataReader _sqldatareader = _sqlcommand.ExecuteReader();
_sqldatareader.Read();

}

But i gotta error. now u got my question. And thanks for your reply.


Thanks in Advance

Dhinesh kumar
Malli_S 17-Aug-12 6:54am
   
Sounds like homework.
Prabhakaran Soundarapandian 17-Aug-12 7:16am
   
What was the error you are facing...can you post your error...

If you want to save the checked items of a list view, you can loop through them using ListView.CheckedItems Property [^]. That should make the code more robust.

So:
- prepare the SQL statement
- loop through the checked items
- on each iteration set the parameters correctly based on the item in hand
- execute the SQL statement (INSERT)

Few things to notice:
- use a transaction for the whole operation
- possibly delete all previously inserted data
- utilize proper error handling and roll back the transaction in case of an error
- never ever concatenate the values, use SqlParameter in your statements
   
for (int i = 0; i < listView1.Items.Count; i++)
           {
           SqlConnection _sqlconnection = new SqlConnection();
           string[] conString1 = DBSettings.connectionStringValues();
           string connectionString1 = string.Format(@"server=" + conString1[0] + ";database=POSNET_ROOM;uid=" + conString1[2] + ";pwd=" + conString1[3]);
           _sqlconnection.ConnectionString = connectionString1;
           _sqlconnection.Open();

           SqlCommand _sqlcommand = new SqlCommand("insert into Listview (RoomType,RoomNumber,GuestName,EnteredDate)values('"+listView1.Items[i].SubItems[0].Text+"','"+listView1.Items[i].SubItems[1].Text+"','"+listView1.Items[i].SubItems[2].Text+"','"+listView1.Items[i].SubItems[3].Text+"')",_sqlconnection);

               SqlDataReader _sqldatareader = _sqlcommand.ExecuteReader();
           _sqldatareader.Read();



Here listView1 -> Name of Listview
Listview -> Table Name
RoomType,RoomNumber,GuestName,EnteredDate -> Columns of Table and listview
   

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900