I have a winform where I have many textboxes and comboboxes. The user fills the data into every text box that I need to add it to my sql query as a where clause.
I have tried to create a list after checking if the user added or selected an item. I need to know how to add this list to my query.
Query without the list:
int val;
int val2;
Int32.TryParse(category.SelectedValue.ToString(), out val);
Int32.TryParse(type.SelectedValue.ToString(), out val2);
SqlConnection conn = new SqlConnection();
conn.ConnectionString = "Data Source=ANTHONY-LAP\\SQLEXPRESS;Initial Catalog=Tenant Management;Integrated Security=True";
conn.Open();
string search_channel = @"select c.[Name],cate.[Channel Category],tp.type,st.Status,c.Surface,
g.GOVERNATOR + ' ' + d.District + ' ' + cit.City + ' ' as [Address],
c.[Short term Price per night] as [Short term monthly amount],
c.[Long term price per month] as [Long term monthly amount],c.[Selling Price]
from[dbo].[Channel] c
inner join[dbo].[Governator] g on c.[Governator ID] = g.ID
inner join[dbo].[District] d on c.[District ID] = d.ID
inner join[dbo].[City] cit on c.[City ID] = cit.id
inner join[dbo].[Channel_Category] cate on c.[Channel Category ID] = cate.ID
inner join[dbo].[Channel_Type] tp on c.[Channel Type] = tp.id
inner join[dbo].[Channel_Status] st on c.[Channel Status] = st.ID
inner join[dbo].[Reservations] r on c.[ID] = r.[Channel ID]
where r.[Actual Date in] < " + res_from.Value.ToString("yyyy/MM/dd") + " and r.[Actual Date out] > " + res_to.Value.ToString("yyyy/MM/dd") +
"and c.[Channel Status]!= '2' and c.[Channel Category ID] =" + val + "and c.[Channel Type] ="
+ val2 + " and c.Surface =" + Convert.ToInt32(surf.Text) +
" and c.[Short term Price per night]= " + Decimal.Parse(shrtrntprice.Text) +
"and c.[Long term price per month]=" + Decimal.Parse(lngrent.Text) + "and c.[Selling Price] =" + Decimal.Parse(sellprc.Text);
This query is working correctly, but I must fill all the controls. I need to know how to make the condition if the user fill the boxes only.