Not retrieving the all selected values from checkbox - c#

Below code is written in such a way to retrieve all selected check box values
But its retieve only the first selected value
Please help
Dim CheckedValues As String
For Each item as ListItem In txt_panview0_ddinput1.Items
If item.Selected Then
CheckedValues = CheckedValues & item.Value
End If
Next
If Not String.IsNullOrEmpty(checkedValues) Then
checkedValues = checkedValues.Substring(1)
End If
tempCollector = tempCollector + "<br>" + "Area Name" + ": " + checkedValues

If I read your code correctly, you're mashing together all of the values from your list into a string, without anything separating them. You therefore have no way of retrieving the original values.
You could try separating your values with a comma before adding them to the string. But there might be a better way to do this. It really depends on what you are trying to do. You might have better luck filling a list object.

Changed CheckedValues = CheckedValues & item.Value
to
CheckedValues += CheckedValues & item.Value perhaps

Related

Combobox selection based on FindStringExact not working as intended

I'm trying to select the item in the Combobox based on the stored value in a Sorteddictionary
String value matching
comboBoxEdit3.SelectedItem = comboBoxEdit3.FindStringExact(Queries[_ucSetting.StandardSearchID.ToString()] + "(" + _ucSetting.StandardSearchID.ToString() + ")");
Elements in ComboBox
But this produces empty selection in the ComboBox
FindStringExact only returns the index of the first item that matches your string, or -1 if no match is found. You're trying to set the SelectedItem to the index being returned. You should set the SelectedIndex instead:
comboBoxEdit3.SelectedIndex = comboBoxEdit3.FindStringExact(Queries[_ucSetting.StandardSearchID.ToString()] + "(" + _ucSetting.StandardSearchID.ToString() + ")");

DatatGrid Itemsource using Linq i cant select any record except first

Hey im currently having problems with WPF Datagrid using linq, currently im displaying a group of records from three tables into a datagrid, this works fine and i retrieve all the relevant information correctly.
However when i load the datagrid and i click on for example the 3rd record it selects the first record and i cant change it. I can use Ctrl + click to deselect the first record.
I dont know why its doing this but ive narrowed it down to my linq query, ive tried to write a more complex linq query using joins etc, it retrieves the same data but i still have this problem :/ any ideas would be good...thank you in advance
apptGrid.ItemsSource = (from o in DbList.OrderedAppointmentList()
from s in DbList.StaffList()
from c in DbList.ClientList()
where o.Appointment_Date == apptDatePicker.SelectedDate.Value
&& o.Staff_Staff_ID == s.Staff_ID && o.Client_Client_ID == c.Client_ID
select new
{
o.Appointment_Date,
o.Appointment_Time,
o.Duration,
StaffName =
((s.Middle_Name_s_ != null) ? s.First_Name + " " + s.Middle_Name_s_ + " " + s.Last_Name : s.First_Name + " " + s.Last_Name),
ClientName =
((c.Middle_Name_s_ != null) ? c.First_Name + " " + c.Middle_Name_s_ + " " + c.Last_Name : c.First_Name + " " + c.Last_Name)
});
Try a ToList() at the end of the query.
I had something similar like you, but to me happen, that when i add rows with the same data information, the selection seems to be crazy. what i did was not give a linq query as a itemsource else put all the information into a List and next pass it to the item source.
"ive tried to write a more complex linq query using joins etc,"
That's never going to help.
Write a program that is human readable (and not just computer readable). And I can bet, you will find the problem and the solution in that iteration.

How to check if string is in column value of returned DataRow array in C#?

I know this is very basic, but I can't seem to get it right. I have this datatable that I will fill with processed information from my database.
After searching if the EmpRequestID has already been added in the Datatable, I want to be able to get the value of the column named "RequestedEmp" of the returned row and check if already contains the initials that my variable is currently hosting (it is in a loop). If it does not, append the initials in the variable in the existing initials in the row.
DataRow[] MyEmpReq_Row = MyEmpRequests_DataTable.Select("EmpRequestID='" + EmpRequestID + "'");
int SameReqID = MyEmpReq_Row.Length;
if (MyEmpReq_Row > 0) //REQ ID IN DT, MULTIPLE EMP 1 REQUEST
{
//INSERT HERE
}
else //ID NOT IN DT YET
{
MyEmpRequests_DataTable.Rows.Add(EmpRequestID, ActionBy, Requested_Initials, DateSubmitted, RequestStatus);
}
I want to be able to do something like this
string RetrievedInitials = MyEmpReq_Row["RequestedEmp"].ToString();
if (RetrievedInitials LIKE '%" + Requested_Initials + "'") // but if statements doesnt have LIKE
or this and then know if the column contains the value or not.
MyEmpReq_Row.Select("RequestedEmp LIKE '%" + Requested_Initials + "'");
if (RetrievedInitials.Contains(Requested_Initials))
Take a look at the string class:
http://msdn.microsoft.com/en-us/library/system.string.aspx
As already mentioned by some other posters, the Contains() method may be of use. However, you should also look at EndsWith() (which is more in line with your use of the wildcard in your LIKE query) and StartsWith().
Example:
if (RetrievedInitials.EndsWith(Requested_Initials))

c# - sql print checked boxes

string filter = string.Empty;
if (checkboxBalkon.Checked== true)
{
filter = "Balkon LIKE '%" + checkboxBalkon.Checked + "%'"; //???
}
I have search form. The code above is supposed to print down all fields from table that has checkbox checked. I compare it, but i don't know how to print it? I need some bool? How to do it?
I believe the answer you are looking for is something like this:
filter = "Balkon = " + checkboxBalkon.Checked;

Store multiple ListBox selections in database?

I have a ListBox called listbox. Its "multiple selection" property is set to true. I need to store the selections from this ListBox into a database field.
Note that I am using web forms, ASP.NET, C#, and Visual Studio Web Developer 2010 Express.
My code is as follows:
SqlCommand insertNL = new SqlCommand("insert into dbo.newsletter (newsletter_subject,newsletter_body,newsletter_sentto) VALUES ('" + TextBox1.Text + "', '" + TextBox2.Text + "', '" + ListBox1.SelectedItem + "')", badersql);
badersql.Open();
insertNL.ExecuteNonQuery();
badersql.Close();
Unfortunately, this code only stores the first selected value of the ListBox in the "newsletter_sentto" column of my newsletter table. Does anyone have any suggestions as to how to fix this code? Thanks in advance.
Things to fix:
Before doing anything else, parameterize your sql. This is ripe for injection. Tutorial here.
You aren't disposing of your cmd or connection string. Wrap those in Using clauses. Example here.
Do a foreach on the items to see which ones are selected. Either store those in a comma separated list in the database (which needs parsing on the way back out) or store them in their own table.
You need to decide how you want to store the multiple "newsletter_sentto" values first.
You're best solution is to create a new child table where you have 1 row and column per selected item, with a foreign key back to your newsletter table.
You can try to store them all together in one row with multiple columns (sentto1, sentto2, etc), this will limit the max number of values you can store, and will cause problems searching across multiple fields. How will you query what was sent to a particular person? WHERE sentto1=#user or sentto2=#user... no index can be used there.
You can stuff all the value in a single row and column using a "," or ";" to separate the values. this will cause many problems because you'll have to constantly split the string apart, every time you need to get at one of the sentto values.
First, you should use parametrized queries instead of straight concatenation to protect against SQL injection. Second, you need to cycle through the selected items and build (presumably) a delimited-list of the selections.
var selectedItems = new List<string>();
foreach( var item in ListBox1.SelectedItems )
selectedItems.Add( item.ToString() );
var sql = "Insert dbo.newsletter(newsletter_subject,newsletter_body,newsletter_sentto)"
+ " Values(#newsletter_subject, #newsletter_body, #newsletter_sentto)"
badersql.Open();
using ( qlCommand = new SqlCommand(sql, badersql) )
{
qlCommand.Parameters.AddWithValue("#newsletter_subject", TextBox1.Text);
qlCommand.Parameters.AddWithValue("#newsletter_body", TextBox2.Text);
qlCommand.Parameters.AddWithValue("#newsletter_sentto", string.Join(',', selectedItems.ToArray()));
qlCommand.ExecuteNonQuery();
};
Try to get selected items like that :
string newsletterSentTo = "";
foreach (ListItem item in ListBox1.Items)
{
if (item.Selected)
newsletterSentTo += "," + item.Text;
}

Categories

Resources