c# - sql print checked boxes - c#

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;

Related

Set value of WinForms Chart label

I am trying to draw a chart from SQL-Data, which currently looks like this:
This is the table db.GetTable(cmdGraph) returns:
How do I manage to change the labeling of the pie-parts to display the value of the field Count so that it looks like this?
This is the code that I use to draw the chart:
private void UpdateEvaluation(SqlCommand cmdGraph)
{
chPie.DataSource = db.GetTable(cmdGraph);
chPie.Series["Series1"].XValueMember = "Value";
chPie.Series["Series1"].YValueMembers = "Count";
chPie.DataBind();
}
And this is how it gets called:
UpdateEvaluation(new SqlCommand("SELECT Value, Count(*) as Count " +
"FROM DV.dbo.tbDefender " +
"WHERE Class LIKE 'Operating System' " +
"AND Type LIKE 'Caption' " +
"GROUP BY Value " +
"ORDER BY Count DESC "));
I found it out myself:
chPie.Series["Series1"].IsValueShownAsLabel = true;
The value IsValueShownAsLabel has to be set. Either programatically or through the Designer (In Properties under Chart Series and then look for IsValueShownAsLabel under Label)

DataTable & Bindingsource unable to cast

I am trying to make a multiple criteria filter in my datagridview using datatable. Only problem is to convert gridview to table, and was trying more answers found here and MSDN forums. It is still not working and now I'm stuck.
Here is my full method:
public void updateText(string txt, string txt2, string txt3)
{
myform = null;
var bs = (BindingSource)dataGridView1.DataSource;
var table = (DataTable)bs.DataSource;
table.Select("Azonosito Like '%" + txt + "%' AND Név Like '%" + txt2 + "%'");
label4.Text = "Lefutott"; //just for debugging reasons to see if it runs
dataGridView1.DataSource = table;
}
Azonosisto an Név are cloumn headers. txt3 is currently not used, might need it in the future, all filter texts came from another form using this method.
You need to call the DataBind method.
I think that if you add this line in the end (after you assign the data source) it should work.
dataGridView1.DataBind();

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# search ms access db by ID

private void Filtriraj()
{
string filter = string.Empty;
if (txtID.Text.Length > 0)
{
filter = "ID LIKE '%" + txtID.Text + "%'";
}
if (txtName.Text.Length > 0)
{
filter = "Name LIKE '%" + txtName.Text + "%'";
}
}
I want to search thru ms access db tables in my c# app. The one above is connected to "Table1" that has some fields like ID, name, surname, address...ID type is set to autonumber, all others are to text. I'm able to search all fields except by ID, this way above wont work, i get exception when i try to search by ID (I type in txtbox some ID number that is in db, exmp: '1')
Search by txtName is working fine.
Autonumber is some form of number (long I think) so you can't use the LIKE keyword. You must search for an exact match (or greather than, less than etc.). You also can't surround a number with single quotes, so those will need to be removed.
I'd switch your code to something like this:
.
.
.
if (txtID.Text.Length > 0)
{
int id;
if (Int32.TryParse(txtID.Text, out id))
{
filter = "ID = " + id.ToString();
}
}
.
.
.
Also, your code looks like it may not work properly if you have multiple text boxes filled with data (because you are not using else if). Whatever text box you check last will end up being the filter that gets used because you're reassigning the filter variable each time. And if you're using the filter text directly from the text boxes, you're opening yourself up to possible SQL Injection. You should probably look into using parameterized queries.

Categories

Resources