Click here to Skip to main content
15,902,911 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
how to pass tables field names in insert commamd of sql which was selected from checkedlistbox in Window Form application using C#

Explanation---

I want to use insert query to insert some values in particular table.But the field names of that table was selected from CheckedListBox...


insert into Tablename(??????????)values()

Instead of ? ihave to select the field names from Checkedlistbox....
Plz help....
Posted
Comments
CHill60 20-Feb-13 5:40am    
You're really going to insert a row onto your table with only 1 column completed? I think your table schema might need some attention. However, the sql command is just a string so you can build it up dynamically. What have you got so far? (i.e. post your code segment)

1 solution

Hi,

you can go for dynamic SQL queries.
prepare the string like below.
C#
string strQuery = "INSERT INTO TableName(";
int count =0;
foreach(object itemChecked in checkedListBox1.CheckedItems)
{
    strQuery += itemChecked.ToString();  //column names from the checked list
    count++;
    if(checkedListBox1.CheckedItems.Count > count)
        strQuery += ","; 
}
strQuery += ")VALUES(";
for(int i =0;i<checkedlistbox1.checkeditems.count; i++)
{
    if(i != 0)
        strQuery +=",";
    strQuery += "Value " + i.ToString(); // your values to the table
}


send that string to stored procedure and in the procedure execute the query like below.
SQL
EXEC(@Query)
--Where @Query would be input parameter


refer links below for more on dynamic sql.
Execute Dynamic SQL commands in SQL Server[^]
Dynamic SQL[^]

hope it helps.
 
Share this answer
 
v2

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