Click here to Skip to main content
15,888,521 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
hi how to save selected listitems via loop in sqltable?
Posted
Comments
Herman<T>.Instance 9-Feb-12 5:05am    
delete current set in table and insert the new set in the table
ythisbug 9-Feb-12 5:07am    
i dint get u?

I assume you have a ListBox
You can create an xml string of selected item in ListBox and that xml string you can send as parameter to your stored procedure and then you can insert your data to your table

Following T-SQL code snippet can give you brief idea for achieving the same


SQL
DECLARE @idoc int
DECLARE @doc varchar(1000)
SET @doc ='
<root>
<customer customerid="VINET" contactname="Paul Henriot">
   <order customerid="VINET" employeeid="5" orderdate="1996-07-04T00:00:00">
      <orderdetail orderid="10248" productid="11" quantity="12" />
      <orderdetail orderid="10248" productid="42" quantity="10" />
   </order>
</customer>
<customer customerid="LILAS" contactname="Carlos Gonzlez">
   <order customerid="LILAS" employeeid="3" orderdate="1996-08-16T00:00:00">
      <orderdetail orderid="10283" productid="72" quantity="3" />
   </order>
</customer>
</root>'
--Create an internal representation of the XML document.
EXEC sp_xml_preparedocument @idoc OUTPUT, @doc
-- INSERT data to table variable using SELECT statement that uses the OPENXML rowset provider.
--Note: You can use your own table here instead of table variable for inserting data from xml
DECLARE @table TABLE
(
CustomerID varchar(10)
,CustomerName varchar(20)
)
INSERT INTO @table
SELECT    *
FROM       OPENXML (@idoc, '/ROOT/Customer',1)
            WITH (CustomerID  varchar(10),
                  ContactName varchar(20))

SELECT * FROM @table


It is for your understanding purpose, If you need any help let me know...
 
Share this answer
 
Comments
ythisbug 9-Feb-12 5:34am    
i want to insert listitems without using xml.can u help?simple way
jai000 9-Feb-12 5:39am    
Can you paste your C# code here? then I will be able to explain you. Paste your code where you are trying save your data
ythisbug 9-Feb-12 5:46am    
SqlConnection conn = new SqlConnection(ConfigurationManager.AppSettings["dbConnection"].ToString());
conn.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "usp_InsertWorkAllocation"
cmd.Connection = conn;
//this for list box...
for (int i = 0; i < lbAddedItems.Items.Count; i++)
{

string selectedItem = lbAddedItems.Items[i].Text;
}
//dropdown how to write code????
jai000 9-Feb-12 5:49am    
Where is your INSERT query? You have to execute your INSERT query inside for loop
ythisbug 9-Feb-12 23:24pm    
jai m using storedprocedure to insert...see i declared..cmd.commantText="usp_InsertWorkAllocation"
Write your code in following way

C#
for (int i = 0; i < lbAddedItems.Items.Count; i++)
        {
         //Note: values of Lid_value, Bid_value and Tid_value will get changed based on your list item
         cmd.Parameters.AddWithValue("@LID", Lid_value);
         cmd.Parameters.AddWithValue("@BID", Bid_value);
         cmd.Parameters.AddWithValue("@TID", Tid_value);
         cmd.ExecuteNonQuery();
        //Clear parameters
         cmd.Parameters.Clear();
        }


I hope this will help you
 
Share this answer
 
Comments
ythisbug 10-Feb-12 4:08am    
its comin error like unassigned value for Lid_value,Bid.....Tid...
jai000 10-Feb-12 4:21am    
what did you do? Can you paste your new code? It seems that you have not assigned values...
ythisbug 10-Feb-12 4:38am    
for (int i = 0; i < lbAddedItems.Items.Count; i++)
{
int Lid_value;
int Bid_value;
int Tid_value;
string selectedItem = lbAddedItems.Items[i].Text;
//cmd.Parameters.Add(pLID);
//cmd.Parameters.Add(pTID);
//cmd.Parameters.Add(pBID);
cmd.Parameters.AddWithValue("@LID", Lid_value);
cmd.Parameters.AddWithValue("@BID", Bid_value);
cmd.Parameters.AddWithValue("@TID", Tid_value);
cmd.ExecuteNonQuery();
//Clear parameters
cmd.Parameters.Clear();
//cmd.ExecuteNonQuery();
}
Try below code and read comment carefully

C#
for (int i = 0; i < lbAddedItems.Items.Count; i++)
        {
            //Try this code and check it's working or not
            //This is not a correct way, you should assign these 
            //values based on your list item
            int Lid_value = 1;
            int Bid_value = 2;
            int Tid_value = 3;

            string selectedItem = lbAddedItems.Items[i].Text;
            //cmd.Parameters.Add(pLID);
            //cmd.Parameters.Add(pTID);
            //cmd.Parameters.Add(pBID);
            cmd.Parameters.AddWithValue("@LID", Lid_value);
            cmd.Parameters.AddWithValue("@BID", Bid_value);
            cmd.Parameters.AddWithValue("@TID", Tid_value);
            cmd.ExecuteNonQuery();
            //Clear parameters
            cmd.Parameters.Clear();
            //cmd.ExecuteNonQuery();
        }
 
Share this answer
 
v2
Comments
ythisbug 10-Feb-12 4:53am    
hey thanks.but here adding 1,2,3 to tat columns..its not adding selected values from list
jai000 10-Feb-12 5:05am    
Is it working?
You are right it will save 1,2,3 every time in your table, just think how can you assign your values to these variables

If it is working fine then click on "Accept Solution" button and accept the answer
ythisbug 10-Feb-12 5:15am    
ya working.bt saving 1,2,3...
for two dropdown list m getting values from othr tables how to get it by using session??
jai000 10-Feb-12 5:20am    
Whatever you asked that has been answered so please end this discussion here..

For session related thing you can post new question... I am not able to help you in same matter.

Thnaks
ythisbug 10-Feb-12 5:43am    
i tried other logic its working for me..thanks for ur help

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