select cheboxlist if any values are selected in sql

hello and thanks in advance, 

I have a checkboxlist that is created dynamically from table1 once the records are inserted they are inserted in table2. i can save the values into the table and remove them if i uncheckd them that is working fine. the problem is to checked the items that have been selected already, how to i do this?? really really confuse...  

here is the cs code;

namespace IMS_2K9.Admin

{

public partial class Order_Charges : System.Web.UI.Page

{

private string connectionString = WebConfigurationManager.ConnectionStrings["IMSConnectionString"].ConnectionString;

 

protected void Page_Load(object sender, EventArgs e)

{

 

}

 

protected void Button1_Click(object sender, EventArgs e)

{

 

SqlConnection con = new SqlConnection(connectionString);

SqlCommand cmd = new SqlCommand("IMS_Insert_OrderCharges", con);

cmd.CommandType = CommandType.StoredProcedure;

con.Open();

 

try

{

CheckBoxList chkbx = (CheckBoxList)form1.FindControl("CheckBoxList1");

 

for (int i = 0; i < CheckBoxList1.Items.Count; i++)

{

 

if (chkbx.Items[i].Selected)

{

cmd.Parameters.AddWithValue(
"@ID", Request.QueryString["id"]);cmd.Parameters.AddWithValue("@ChargeType", CheckBoxList1.Items[i].Value.ToString());

cmd.ExecuteNonQuery();

cmd.Parameters.Clear();

}

if (chkbx.Items[i].Selected == false)

{

cmd.Parameters.AddWithValue(
"@ID", Request.QueryString["id"]);

cmd.Parameters.AddWithValue("@ChargeType", CheckBoxList1.Items[i].Value);

cmd.Parameters.AddWithValue("@Action", "Remove");

cmd.ExecuteNonQuery();

cmd.Parameters.Clear();

 

}

}

}

catch

{

ClientScript.RegisterStartupScript(
typeof(Page), "ShowMessage", "<script language='JavaScript'>alert('An error occurred on the app please try again in a few minutes');</script>");

}

finally

{

con.Close();

ClientScript.RegisterStartupScript(
typeof(Page), "ShowMessage", "<script language='JavaScript'>alert('Charges Submitted Thanks..');</script>");

 

}

}

}

}

here is the asp checkboxlist code:

<asp:CheckBoxList ID="CheckBoxList1" runat="server"

DataSourceID="SqlDataSource1" DataTextField="Charge" DataValueField="ID"

RepeatColumns="2">

</asp:CheckBoxList>

 

 

 

0
mitoy75
2/10/2009 6:46:55 PM
asp.net.getting-started 91979 articles. 4 followers. Follow

3 Replies
722 Views

Similar Articles

[PageSpeed] 54
Get it on Google Play
Get it on Apple App Store

You can set the selectedvalue property in the code-behind if it's just one item.  If it's multiple items, you can iterate though the Items collection setting the Selected property as needed.


C# <---> VB.Net Translator
0
MetalAsp
2/10/2009 7:44:31 PM

can you post an example.. thanks

0
mitoy75
2/10/2009 7:55:28 PM

MetalAsp.Net:

You can set the selectedvalue property in the code-behind if it's just one item.  If it's multiple items, you can iterate though the Items collection setting the Selected property as needed.

Hi,

I would like to share this example with you. It uses a List of bool value to represent the data in you table. Have a try on it to see if it helps. 

<%@ Page Language="C#" %>
<%@ Import Namespace="System.Collections.Generic" %>
<script runat="server">
    protected void Page_Load(object sender, EventArgs e)
    {
        List<bool> lstChk = new List<bool>();
        lstChk.Add(true);
        lstChk.Add(false);
        lstChk.Add(true);
        lstChk.Add(true);
        lstChk.Add(false);
        
        for(int i=0;i<lstChk.Count;i++)
        {
            chk.Items.Add(new ListItem("item" + i.ToString()));
        }

        for (int i = 0; i < lstChk.Count; i++)
        {
            if (lstChk[i] == true)
                chk.Items[i].Selected = true;
        }
    }
html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title></title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:CheckBoxList ID="chk" runat="server">
        </asp:CheckBoxList>
    </div>
    </form>
</body>
</html>
Best Regards,
Shengqing Yang
Please remember to click “Mark as Answer” on the post that helps you, and to click “Unmark as Answer” if a marked post does not actually answer your question. This can be beneficial to other community members reading the thread : )
0
Shengqing
2/13/2009 6:50:54 AM
Reply:

Similar Artilces:

getting a value with sql select
hello community,I think my problem should have a very simple solution. It seems to me a very simple task, but I could not find an solution for my asp.net page.On a web page I placed a SqlDataSource with a simple SQL-Select like this:        <asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:ConnectionString %>"            SelectCommand="SELECT [some_value] FROM [TabValues] WHERE [id] = @id">            .....

SELECT from SELECT
Hi! I remember seeing an example where you can do a select from a nested select, but I cannot seem to make it work. So it would be something like this: select max( name ) from ( select name from customer union select name from vendor ) Was I dreaming I saw that? Thanks! D In article <4500243f@forums-2-dub>, daniel_AT_infrontsoftware_DOT_com@N0SPAM.com says... > Hi! > > I remember seeing an example where you can do a select from a nested > select, but I cannot seem to make it work. No, you weren't. Try (UNTESTED!) adding a...

Using selected values from a Multi Select Listbox in a SQL query
I have seen a few post concerning this but don't really understand fully as I am new to this.  I would appreciate any help 1) First I loop through the Listbox control and get the selected values: Dim li As ListItemFor Each li In lbPClass.Items If li.Selected = True Then strPC += li.Value & ", " End If Next Label5.Text = strPC The resulting string looks something like: 101, 102, 100 ,    I get rid of the last space and "," with the following statement Label6.Text = Left(Label5.Text, Trim(Len(Label5.Text) - 2)) 2) I would then like to ...

getting selected data in grid view according to the selected value in drop down
hi! how can i change the grid view data according to the selected value in the drop down list? Make the DataSource the GridView is bound too have a select statement that uses the value of the dropdownlist as a parameter. hth, mcm  Bind your grid in your drop down selectedindexchange and reference the value being selected in your query likeprotected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e){        string val = DropDownList1.SelectedItem.Text;        //Code for populating your grid here like ...

Dropdownlist selected value passes first value in list no matter what value is selected. why ?
 HI . I have a dropdownlist. Databinds ok but when I must pass a value as a Selected value it always passes the first value in the list even if I select another value. I tried to set the ddl to enableviewstate false but it doesn't change anything. Any ideas ? Code below, thanksLoading the ddl:       productCategories = CatalogAdmin.Admin_Product_Categories_NotIn(productId);        for (int i = 0; i < productCategories.Rows.Count; i++)        {       &...

DROPDOWNLIST DOES NOT SELECT THE SELECTED VALUE
Hi all, i am using C# , asp.net vs2005. ajax 1.0 i am populating data programitically . when i select first value, dropdown control select the same. when i select second value ,dropdownlist select second value too But i does not continue  from third value onward . when i select third value, dropdownlist control select second value then after behavior reman the same no matter what i select dropdownlist control select second value ONLY.<asp:DropDownList id="Ddlzse" runat="server" Width="165px" Font-Bold="False" Fo...

Accessing the selected value from a Select
Need to access the selected value from a Select in client script.  I can get the selectedIndex but where do I find the selected value? Will this give it to me? form1.Select1.Options(form1.Select1.SelectedIndex)   form1.Select1.options[form1.Select1.selectedIndex] Try form1.Select1.options[form1.Select1.selectedIndex].value> penny for a thought...

Getting DropDownlist's DataTextValue Selected Value Given the SelectedValue and/or Selected Item
In the vb.net codebehind, I need to extract something from the Text (displayed) value of dropdownlist's selected item and I have  the SelectedValuand and SelectedItem in hand. How can I do this?  FindControl? Item? row?   Thanks. Well, its easier than you might think. SelectedItem has a "text" property. Thats the selected text right there :)  If you need the Text of the selection, then you have to use the SelectedItem.Text property.  If you want the value (not the displayed text) the you can directly use the SelectedValue. dim sText as String = dropdown...

get value from <select> value & display value
hi all, i want these two bold values which are below,  underline value i can get but second bold value i m not avail to get please help.... <select><option value="ma">manoj</option> </select> regard khushak  P.S. if possible then Please with  java script other wise code behind New In .NET World See my post in this thread.  That should take care of you. Peter BrunoneMS MVP, ASP.NETFounder, EasyListBox.comDo the impossible, and go home early....

Second SELECT-box doesn't show the selected value as SELECTED
Hi! I usually find what I look for in the other threads, but now I've spent two weeks searching and I still can't work this out, even though I'm sure the solution is easy:( Why doesn't my second dropdown-list show the selected value Arb (which shows in the url-window as for example myPage.asp?Ar=1976&Arb=1987) as selected? It manages to use the Ar-variable for the first SELECT-box, but the second box always has the first querystring-value selected, as if Arb="". I've also tried using a second sql-query, doing exactly as with the first SELECT-box, without success. Is ...

Selected value in select list with javascript based off value in database
 I've got a classic asp page with a select list. I need the select list's selected value to be whatever the user selected when they created their account. I've been trying to use <body onLoad="onLoad();"> to call my function, and then I'm trying to specify which value should be selected in the function. Also, when the select list selected values change, there is a javascript function I call to disable text boxes. I think there is an issue with my javascript function, and I can't find any examples of this from good ole' google.  My j...

Cascading Dropdown - Initially selected value
Hi there, I am having an issue with a pretty basic problem. I have managed to get the cascading drop down lists working ok with information coming from a database. On the initial page, I need to have a value in the drop down list already selected, i.e. the current year in one drop down and current month in the next. The information from both of these is sent to a query to output a datatable. Can anyone help with this? I'm not sure I understand the scenario, but I think the new CascadingDropDownNameValue(..., defaultValue) constructor may be just what you're looking for. (First available ...

get value from SQL server 2005 select statement with datareader
I just want a simple datareader, that i can read the value returned from a select statement executed on a SQL server 2005 db.  The code below should work in, but email[calc]= rdr[0].ToString(); when i want to read some data a get a exception saying:System.InvalidOperationException was unhandled by user code  Message="Invalid attempt to read when no data is present."  Source="System.Data"  StackTrace:       at System.Data.SqlClient.SqlDataReader.GetValue(Int32 i)       at System.Data.SqlClient.SqlDataReader.get_It...

How to get the selected text HREF value using vb.net
Hello friends,                     If i have a page opened in a web browser control for example www.google.com .if i select a particular content in google web page i have to get the href value of the selected text.how to do this vb.net.can anyone help me. with regards, Balaji.   Balaji,Chennai...

Web resources about - select cheboxlist if any values are selected in sql - asp.net.getting-started

Resources last updated: 11/28/2015 11:31:29 AM