asp.net GridView and Checkboxes Dynamic Bind - asp.net

I am having a little issue that I don't seem to understand the best way to approach.
I have a GridView that get automatic column generations based on the query I run. The GridView will contain (Name) (Description) (Edit) (Delete) (View) (Admin).
Now because the Edit, Delete, View... are bit's in the database when the query returns the results and binds the data with the GridView I get these grayed out Checkboxes with checked if True or Unchecked if False.
Now because I didn't create those disabled checkboxes are they really a checkbox or are the something that's just display like that... If they are really a checkboxes how do I access them and enable or disable them? I tried looping through each cell in grid but when I say cell.text it gives me empty string back... What would be the best way to approach this or am I misunderstanding the DataBind of a bit fields?
Thanks all for your help.
UPDATED
string sSQLAccess = "SELECT ap.n_Name 'App', a.b_Edit 'Edit', a.b_Delete 'Delete', a.b_View 'View' " + Environment.NewLine
+ "FROM tbl_Actions a " + Environment.NewLine
+ "JOIN tbl_Applications ap ON ap.u_ID = a.u_ApplicationID" + Environment.NewLine
+ "JOIN tbl_Roles r ON r.u_ID = a.u_RoleID" + Environment.NewLine
+ "WHERE a.b_Deleted = 0" + Environment.NewLine
+ "AND ap.b_Deleted = 0 " + Environment.NewLine
+ "AND r.b_Deleted = 0 " + Environment.NewLine
+ "AND a.u_RoleID = '" + Request.QueryString["ID"] + "'" + Environment.NewLine;
grdAccess.DataSource = vwAccess;
grdAccess.DataBind();

The checkbox will not be enabled unless the gridview is in edit mode - you would need to define an edit template for the gridview.

Related

ASP.NET Retrieve value from Checkbox onCheckChanged

I am working on displaying a students timetable based on possible Module choices. (See Screenshot) Currently I am displaying all available modules in a gridview which the student can select by using check boxes. I have already created the insert query to insert their selection to the database. (2)
However, each time a check box is selected I want to retrieve the 'ModuleId' to add to a SELECT query which displays a timetable of their selected modules. (1)
So if a user selects 3 check boxes the 'ModuleId' from each row selected will be passed into the SELECT query.
Below is my method which enables the timetable for specific a ModuleId to be displayed:
public String[] getModulesAtCurrentSlot(int timeslotInt, String moduleID, String Day)
{
List<String> modulesList = new List<string>();
if (conn.State.ToString() == "Closed")
{
conn.Open();
}
SqlCommand newCmd = conn.CreateCommand();
newCmd.Connection = conn;
newCmd.CommandType = CommandType.Text;
newCmd.CommandText = "SELECT DISTINCT Module.ModuleCode,ClassType.ClassTypeName,Convert(time,Class.StartTime), Convert(time,Class.EndTime),Building.BuildingName,RoomCode.RoomCode,Class.Color" +
" FROM Class INNER JOIN Module ON Class.ModuleId = Module.ModuleId INNER JOIN RoomCode ON Class.RoomCodeId = RoomCode.RoomcodeId INNER JOIN Building ON RoomCode.BuildingId = Building.BuildingId INNER JOIN Days ON Class.DayId = Days.DayID INNER JOIN ClassType ON Class.ClassTypeId = ClassType.ClassTypeId WHERE " +
" Module.ModuleId = " + moduleID + " AND Convert(Date,StartTime) = '" + Day + "' AND " + timeslotInt.ToString() + " BETWEEN ClassScheduleStartTimeId and ClassScheduleEndTimeId";
SqlDataReader dr = newCmd.ExecuteReader();
while (dr.Read())
{
String current = "<div class='slot' " + (!dr.IsDBNull(6) ? "style=\"background-color: " + dr.GetString(6) + ";\"" : "") + ">";
current += "<div class='line1'>" + dr.GetString(0) + " " + dr.GetString(1) + "</div>";// +"<br />";
current += "<div class='line2'>" + dr.GetTimeSpan(2).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + " - " + dr.GetTimeSpan(3).ToString().TrimEnd('0').TrimEnd('0').TrimEnd(':') + "</div>";// +"<br />";
current += "<div class='line3'>" + dr.GetString(4) + " " + dr.GetString(5) + "</div>";
current += "</div>";
modulesList.Add(current);
}
conn.Close();
return modulesList.ToArray();
}
How would I pass all selected values (ModuleId) from the checkboxes to be used in my above select query as previously for displaying just one module I used
' Module.ModuleId = " + moduleID ' ?
To complete what you are trying to do efficiently and without big foreach loops you need to change the whole way your user inputs data in to the gridview.
I believe that you should be utilising the GridViews OnRowUpdating Event, When a row is updated, using this event you already have context of which row has changed. e.RowIndex - Gets the index of the row being updated
Utilising this event will require a bit of a redesign of your gridview fields, You will need to add a EditItemTemplate tag in each TemplateField Add a CommandFieldand using OnRowUpdating re-write your update event. Its to much information to post in a answer, This information should help you get started on using the GridView events to their full potential.
On a side note you should use paramerterized queries in your sql.
EDIT
Based on your clarification that you need to retrieve the moduleid for each checkbox, I am going to make a few assumptions so it might not work without some tweaks.
To start with try this:
<asp:TemplateField>
<ItemStyle HorizontalAlign="Center" Width="40px"></ItemStyle>
<ItemTemplate>
<asp:CheckBox ID="chkRow" runat="server" ToolTip='<%# Eval("ModuleId") %>' OnCheckedChanged="module_Changed" />
</ItemTemplate>
Using '<%# Eval("ModuleId") %>' Will populate the tooltip for the checkbox with the ModuleID value during the GridView.DataBind() function. Iam making the assumption that the column name is exactly "ModuleId"
Then in the code behind you can read the tooltip value of the checkbox like this:
protected void module_Changed(object sender, EventArgs e)
{
// Retrieve the check box ModuleId value to add to SELECT query
string moduleid = ((CheckBox)sender).ToolTip;
}
Now that when the gridview loads all the checkboxes have a tooltip that is populated with the ModuleID you can easily know which box is for which module by checking the tooltip.

How to add newline within one radio button control in VB

UPDATE: (working code attached)
I remove the NewLine / Environment.NewLine, and replaced them by adding <br/> in the "Session :",
which meant by "<br/> Session :", worked like a charm! Thanks #nelek
With rdlSession.Items
.Add("Session :" + session.ToString() + Environment.NewLine + _
"Date :" + day.ToString() + vbNewLine + _
"Coach :" + coach.ToString() + vbNewLine + _
"Available slot :" + slot.ToString())
End With
How to arrange them into four rows instead of bulking up within one row?
They are one of my options in radiobuttonlist. I have tried using Environment.Newline , vbNewline , NewLine.
Is there any possible way to make it?
UPDATE: (working code attached)
I remove the NewLine / Environment.NewLine, and replaced them by adding <br/> in the "Session :",
which meant by "<br/> Session :" and it worked like a charm! Thanks #nelek

Blank Items in CheckBoxList (Remove blank items)

I have a rather irritating and silly problem. I have a checkbox list on a asp page that I populate from a database. I am able to populate it, the problem lies when I do a specific check that checks if the users are active or not, it displays only the ones that are active but then leaves huge blanks in my control of where the original not active users were displayed.
I have pictures of before and after I implement that specific if statement:
here is the code for populating and checking:
this.AddMultipleUsers.Items.Clear();
foreach (GetAllLoginUsersResult result in from a in this.db.GetAllLoginUsers(null)
orderby a.FirstName
select a)
{
ListItem item = new ListItem();
string str = Membership.GetUser(result.UserId).ToString();
item.Text = result.FirstName.Trim() + " " + result.Surname.Trim() + " (" + str + ")";
if (!result.IsApproved)
{
item.Text = item.Text + " (Not Active)";
//item.Attributes.Add("style", "display:none;"); before
}
item.Value = result.UserId.ToString();
this.AddMultipleUsers.Items.Add(item);
}
in the first image, the checkboxlist is fully populated. Before link to code^
in the second after I un-comment this line //item.Attributes.Add("style", "display:none;");
then checkboxlist is the same size as the first image but, there is large spaces between
the users that are active, when you scroll down you see them randomly.
I want to remove the blank items within the checkbox list and make the other valid entries to be moved up like a normally populated checkbox list
Thank you
simply add a where condition to your select statement:
this.AddMultipleUsers.Items.Clear();
foreach (GetAllLoginUsersResult result in from a in this.db.GetAllLoginUsers(null)
orderby a.FirstName
where a.IsApproved==true
select a)
{
ListItem item = new ListItem();
string str = Membership.GetUser(result.UserId).ToString();
item.Text = result.FirstName.Trim() + " " + result.Surname.Trim() + " (" + str + ")";
item.Value = result.UserId.ToString();
this.AddMultipleUsers.Items.Add(item);
}
now you are only cycling through the active users, and no longer need to hide the inactive ones.

Asp.net add to database

Is it possible to add from a single textbox to different tables in a database.
I have a addclub webform and i would like to add clubname to a number of different tables.
The following is my current code that i have.
cmd = new SqlCommand("insert into youthclublist(youthclubname, description, address1, address2, county, postcode, email, phone) values ('" + youthclubname.Text + "', '" + description.Text + "','" + address1.Text + "','" + address2.Text + "', '" + county.Text + "', '" + postcode.Text + "', '" + email.Text + "', '" + phone.Text + "')", connection);
It is possible yes but consider my point below:
Think about normal form, it should be possible to design the database so that you only need to alter it on one place. If you have to change the name in several places it is likely not following normal form and the database could be redesigned.
The way you are doing the update is not advisable, have a look into SQLInjection attacks as the above code is vulnerable to this. Using parameters in the SQLCommand rather than creating a big string is a better way to do this from a security and performance point.
Hope I have not been too negative
Andy
You can do it if you use a transaction (and you should use parameters to get around some SQL injection attack problems) like so (this means that all the inserts are done in a single block on the database which is safer than doing them one after the other):
using (var cmd = new SqlCommand("BEGIN TRANSACTION;"
+ "INSERT INTO youthclublist(youthclubname) VALUES (#youthclubname);"
// Add all your INSERT statements here for the other tables in a similar way to above (I simplified it to show just one column, but you get the idea)
+ "COMMIT;", conn))
{
// Add your parameters - one for each of your text boxes
cmd.Parameters.Add("#youthclubname", SqlDbType.NVarChar).Value = youthclubname.Text;
// execute the transaction
cmd.ExecuteNonQuery();
}

How to search for same search string in multiple columns?

I am searching for string input from textbox control in EF data model. Could someone help me to construct the where clause in the code file to do this.
I tried the below piece of code in the code, even though it compiles throws an error when I enter the search string and submit the search.
Where("it.[CaseName] like '%'" + searchString +
"'%' or it.[CaseNumber] like '%'" + searchString +
"'%' or it.[AppRegNumber] like '%'" + searchString +
"'%' or it.[SSNo] like '%'" + searchString + "'%'")
When this same where clause was used in EntityDataSource control markup it accepts and search correctly.
I am in the process of changing my pages to use ObjectDataSource control to connect to a business logic layer class instead of directly to EDM data model.
If you want to use the Where Query Builder method of ObjectQuery (I think that's what you are using in your question), you need to remove the inner single quotes from the query expression:
Where("it.[CaseName] like '%" + searchString +
"%' or it.[CaseNumber] like '%" + searchString +
"%' or it.[AppRegNumber] like '%" + searchString +
"%' or it.[SSNo] like '%" + searchString + "%'")
Or as a parametrized query:
Where("it.[CaseName] like #search" +
" or it.[CaseNumber] like #search" +
" or it.[AppRegNumber] like #search" +
" or it.[SSNo] like #search",
new ObjectParameter("search", string.Concat("%", searchString, "%")))
Edit
Don't use the first version if searchString comes from user input on a web page as this version is vulnerable to SQL Injection (enter this term in search engine for explanation). Instead use the parametrized query in the second version.
Well, you can use the Linq extension methods:
.Where(r => r.CaseName.IndexOf(searchString) >= 0
|| r.CaseNumber.IndexOf(searchString) >= 0
|| r.AppRegNumber.IndexOf(searchString) >= 0
|| r.SSNo.IndexOf(searchString) >= 0)

Resources