And it sends the dropdown value to another page with this Select query:
@{
var db = Database.Open("dbConn");
var ip = "%" + Request["ip"] + "%";
var data = db.Query("SELECT * FROM Invent WHERE ip like @0 ORDER BY IP", ip);
var grid = new WebGrid(source: data,
defaultSort: "IP",
rowsPerPage: 254);
}
I would like the checkboxes to be checked at the beginning and i would like the value to be inserted in the Select query instead of using *.
You will need to check if each box has been ticked, then add them to the SQL:
@{
if(IsPost){
var fields = new List<string>();
if(!Request["ITKontakt"].IsEmpty()){
fields.Add("ITKontakt");
}
if(!Request["IP"].IsEmpty()){
fields.Add("IP");
}
if(!Request["Mname"].IsEmpty()){
fields.Add("Mname");
}
if(!Request["Type"].IsEmpty()){
fields.Add("Type");
}
if(!Request["Appkontakt"].IsEmpty()){
fields.Add("Appkontakt");
}
//etc
var sql = "SELECT " + String.Join(", ",fields) + " FROM Invent WHERE ip like @0 ORDER BY IP";
}
}
Thanks for the help but im getting some errors when i try this, here is the code im using:
@{
if(IsPost){
var fields = new List<string>();
if(!Request["ITKontakt"].IsEmpty()){
fields.Add("ITKontakt");
}
if(!Request["IP"].IsEmpty()){
fields.Add("IP");
}
if(!Request["Mname"].IsEmpty()){
fields.Add("Mname");
}
if(!Request["Type"].IsEmpty()){
fields.Add("Type");
}
if(!Request["Appkontakt"].IsEmpty()){
fields.Add("Appkontakt");
}
if(!Request["SQL"].IsEmpty()){
fields.Add("SQL");
}
if(!Request["IIS"].IsEmpty()){
fields.Add("IIS");
}
if(!Request["FTP"].IsEmpty()){
fields.Add("FTP");
}
if(!Request["FO"].IsEmpty()){
fields.Add("FO");
}
if(!Request["Beskrivning"].IsEmpty()){
fields.Add("Beskrivning");
}
var db = Database.Open("dbConn");
var IP = "%" + Request["IP"] + "%";
var data = db.Query("SELECT " + String.Join(", ",fields) + " FROM Invent WHERE IP like @0 ORDER BY IP", IP);
var grid = new WebGrid(source: data,
defaultSort: "IP",
rowsPerPage: 254);
}
}
and here is the output-error:
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0103: The name 'grid' does not exist in the current context
Source Error:
Line 66: <br/><br/>
Line 67: </font>
Line 68: @grid.GetHtml(tableStyle: "grid",
Line 69: headerStyle: "head",
Line 70: alternatingRowStyle: "alt",
Source File: c:\Users\Administrator\Documents\My Web Sites\ITInvent\Members\IpList.cshtml Line: 68
Compilation Error
Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately.
Compiler Error Message: CS0136: A local variable named 'grid' cannot be declared in this scope because it would give a different meaning to 'grid', which is already used in a 'parent or current' scope to denote something else
Source Error:
Line 39: var IP = "%" + Request["IP"] + "%";
Line 40: var data = db.Query("SELECT " + String.Join(", ",fields) + " FROM Invent WHERE IP like @0 ORDER BY IP", IP);
Line 41: var grid = new WebGrid(source: data,
Line 42: defaultSort: "IP",
Line 43: rowsPerPage: 254);
Thanks for all the help, but i get another error after this, it says Invalid column name. But there is a column with that name and it works without the changes from this thread.
Server Error in '/' Application.
--------------------------------------------------------------------------------
Invalid column name "Mname".
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.
Exception Details: System.InvalidOperationException: Invalid column name "Mname".
Source Error:
Line 72: @if(grid != null){
Line 73:
Line 74: @grid.GetHtml(tableStyle: "grid",
Line 75: headerStyle: "head",
Line 76: alternatingRowStyle: "alt",
Source File: c:\Users\Administrator\Documents\My Web Sites\ITInvent\Members\IpList.cshtml Line: 74
Stack Trace:
[InvalidOperationException: Invalid column name "Mname".]
WebMatrix.Data.DynamicRecord.VerifyColumn(String name) +20969
WebMatrix.Data.DynamicRecord.get_Item(String name) +17
WebMatrix.Data.DynamicRecord.TryGetMember(GetMemberBinder binder, Object& result) +10
CallSite.Target(Closure , CallSite , Object ) +130
System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +438
Microsoft.Internal.Web.Utils.DynamicHelper.GetMemberValue(Object obj, String memberName) +29
Microsoft.Internal.Web.Utils.DynamicHelper.TryGetMemberValue(Object obj, String memberName, Object& result) +32
System.Web.Helpers.WebGridRow.TryGetMember(String memberName, Object& result) +59
System.Web.Helpers.WebGridRow.get_Item(String name) +58
System.Web.Helpers.WebGrid.GetTableBodyHtml(IEnumerable`1 columns, String rowStyle, String alternatingRowStyle, String selectedRowStyle) +343
System.Web.Helpers.WebGrid.Table(String tableStyle, String headerStyle, String footerStyle, String rowStyle, String alternatingRowStyle, String selectedRowStyle, String caption, Boolean displayHeader, Boolean fillEmptyRows, String emptyRowCellValue, IEnumerable`1 columns, IEnumerable`1 exclusions, Func`2 footer, Object htmlAttributes) +1013
System.Web.Helpers.WebGrid.GetHtml(String tableStyle, String headerStyle, String footerStyle, String rowStyle, String alternatingRowStyle, String selectedRowStyle, String caption, Boolean displayHeader, Boolean fillEmptyRows, String emptyRowCellValue, IEnumerable`1 columns, IEnumerable`1 exclusions, WebGridPagerModes mode, String firstText, String previousText, String nextText, String lastText, Int32 numericLinksCount, Object htmlAttributes) +195
ASP._Page_Members_IpList_cshtml.Execute() in c:\Users\Administrator\Documents\My Web Sites\ITInvent\Members\IpList.cshtml:74
System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207
System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68
System.Web.WebPages.WebPage.ExecutePageHierarchy() +156
System.Web.WebPages.StartPage.RunPage() +19
System.Web.WebPages.StartPage.ExecutePageHierarchy() +65
System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76
System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249
PMSLEET
Member
38 Points
19 Posts
Checkboxes and Select query
Mar 08, 2012 09:22 AM|LINK
Hi again!
I would like some help with checkboxes and Select query.
I have a page that looks like this:
<!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8" /> <title></title> </head> <body> <table> <tr><td valign="bottom"><form action="listproducts.cshtml" method="post"> <select name="ip" style='width:215px'> <option value=" "> </option> <option value="10.16.100.">10.16.100.x - Servernät</option> <option value="10.16.200.">10.16.200.x - Servernät</option> </select> <input type="submit" value="Visa"/> </form> </td> </tr> <tr> <td> <form> <input type="checkbox" name="ITKontakt" value="ITKontakt" /> ITKontaktperson | <input type="checkbox" name="IP" value="IP" /> IP-adress | <input type="checkbox" name="Mname" value="Mname" /> Maskinnamn | <input type="checkbox" name="Type" value="Type" /> Typ | <input type="checkbox" name="Appkontakt" value="Appkontakt" /> Applikationskontakt | <input type="checkbox" name="SQL" value="SQL" /> SQL | <input type="checkbox" name="IIS" value="IIS" /> IIS | <input type="checkbox" name="FTP" value="FTP" /> FTP | <input type="checkbox" name="FO" value="FO" /> Förvaltningsobjekt | <input type="checkbox" name="Beskrivning" value="Beskrivning" /> Beskrivning </form> </td> </tr> </table> </body> </html>And it sends the dropdown value to another page with this Select query:
@{ var db = Database.Open("dbConn"); var ip = "%" + Request["ip"] + "%"; var data = db.Query("SELECT * FROM Invent WHERE ip like @0 ORDER BY IP", ip); var grid = new WebGrid(source: data, defaultSort: "IP", rowsPerPage: 254); }I would like the checkboxes to be checked at the beginning and i would like the value to be inserted in the Select query instead of using *.
Any ideas?
Regards PMSLEET
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Checkboxes and Select query
Mar 08, 2012 12:51 PM|LINK
You will need to check if each box has been ticked, then add them to the SQL:
@{ if(IsPost){ var fields = new List<string>(); if(!Request["ITKontakt"].IsEmpty()){ fields.Add("ITKontakt"); } if(!Request["IP"].IsEmpty()){ fields.Add("IP"); } if(!Request["Mname"].IsEmpty()){ fields.Add("Mname"); } if(!Request["Type"].IsEmpty()){ fields.Add("Type"); } if(!Request["Appkontakt"].IsEmpty()){ fields.Add("Appkontakt"); } //etc var sql = "SELECT " + String.Join(", ",fields) + " FROM Invent WHERE ip like @0 ORDER BY IP"; } }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
PMSLEET
Member
38 Points
19 Posts
Re: Checkboxes and Select query
Mar 12, 2012 12:14 PM|LINK
Hi!
Thanks for the help but im getting some errors when i try this, here is the code im using:
@{ if(IsPost){ var fields = new List<string>(); if(!Request["ITKontakt"].IsEmpty()){ fields.Add("ITKontakt"); } if(!Request["IP"].IsEmpty()){ fields.Add("IP"); } if(!Request["Mname"].IsEmpty()){ fields.Add("Mname"); } if(!Request["Type"].IsEmpty()){ fields.Add("Type"); } if(!Request["Appkontakt"].IsEmpty()){ fields.Add("Appkontakt"); } if(!Request["SQL"].IsEmpty()){ fields.Add("SQL"); } if(!Request["IIS"].IsEmpty()){ fields.Add("IIS"); } if(!Request["FTP"].IsEmpty()){ fields.Add("FTP"); } if(!Request["FO"].IsEmpty()){ fields.Add("FO"); } if(!Request["Beskrivning"].IsEmpty()){ fields.Add("Beskrivning"); } var db = Database.Open("dbConn"); var IP = "%" + Request["IP"] + "%"; var data = db.Query("SELECT " + String.Join(", ",fields) + " FROM Invent WHERE IP like @0 ORDER BY IP", IP); var grid = new WebGrid(source: data, defaultSort: "IP", rowsPerPage: 254); } }and here is the output-error:
Regards PMSLEET
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Checkboxes and Select query
Mar 12, 2012 01:37 PM|LINK
You need to declare the WebGrid before IsPost:
@{ WebGrid grid = null; if(IsPost){ var fields = new List<string>(); if(!Request["ITKontakt"].IsEmpty()){ fields.Add("ITKontakt"); //etcThen in your code where the error occurs:
@if(grid != null){ @grid.GetHtml(... }Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
PMSLEET
Member
38 Points
19 Posts
Re: Checkboxes and Select query
Mar 13, 2012 07:35 AM|LINK
Hello!
I get another error when i try this:
Compilation Error Description: An error occurred during the compilation of a resource required to service this request. Please review the following specific error details and modify your source code appropriately. Compiler Error Message: CS0136: A local variable named 'grid' cannot be declared in this scope because it would give a different meaning to 'grid', which is already used in a 'parent or current' scope to denote something else Source Error: Line 39: var IP = "%" + Request["IP"] + "%"; Line 40: var data = db.Query("SELECT " + String.Join(", ",fields) + " FROM Invent WHERE IP like @0 ORDER BY IP", IP); Line 41: var grid = new WebGrid(source: data, Line 42: defaultSort: "IP", Line 43: rowsPerPage: 254);Any ideas?
Regard PMSLEET
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Checkboxes and Select query
Mar 13, 2012 07:37 AM|LINK
Once you have declared a variable with the name "grid" you can't declare another one. Remove the word "var" on line 41.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter
PMSLEET
Member
38 Points
19 Posts
Re: Checkboxes and Select query
Mar 14, 2012 11:00 AM|LINK
Thanks for all the help, but i get another error after this, it says Invalid column name. But there is a column with that name and it works without the changes from this thread.
Server Error in '/' Application. -------------------------------------------------------------------------------- Invalid column name "Mname". Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code. Exception Details: System.InvalidOperationException: Invalid column name "Mname". Source Error: Line 72: @if(grid != null){ Line 73: Line 74: @grid.GetHtml(tableStyle: "grid", Line 75: headerStyle: "head", Line 76: alternatingRowStyle: "alt", Source File: c:\Users\Administrator\Documents\My Web Sites\ITInvent\Members\IpList.cshtml Line: 74 Stack Trace: [InvalidOperationException: Invalid column name "Mname".] WebMatrix.Data.DynamicRecord.VerifyColumn(String name) +20969 WebMatrix.Data.DynamicRecord.get_Item(String name) +17 WebMatrix.Data.DynamicRecord.TryGetMember(GetMemberBinder binder, Object& result) +10 CallSite.Target(Closure , CallSite , Object ) +130 System.Dynamic.UpdateDelegates.UpdateAndExecute1(CallSite site, T0 arg0) +438 Microsoft.Internal.Web.Utils.DynamicHelper.GetMemberValue(Object obj, String memberName) +29 Microsoft.Internal.Web.Utils.DynamicHelper.TryGetMemberValue(Object obj, String memberName, Object& result) +32 System.Web.Helpers.WebGridRow.TryGetMember(String memberName, Object& result) +59 System.Web.Helpers.WebGridRow.get_Item(String name) +58 System.Web.Helpers.WebGrid.GetTableBodyHtml(IEnumerable`1 columns, String rowStyle, String alternatingRowStyle, String selectedRowStyle) +343 System.Web.Helpers.WebGrid.Table(String tableStyle, String headerStyle, String footerStyle, String rowStyle, String alternatingRowStyle, String selectedRowStyle, String caption, Boolean displayHeader, Boolean fillEmptyRows, String emptyRowCellValue, IEnumerable`1 columns, IEnumerable`1 exclusions, Func`2 footer, Object htmlAttributes) +1013 System.Web.Helpers.WebGrid.GetHtml(String tableStyle, String headerStyle, String footerStyle, String rowStyle, String alternatingRowStyle, String selectedRowStyle, String caption, Boolean displayHeader, Boolean fillEmptyRows, String emptyRowCellValue, IEnumerable`1 columns, IEnumerable`1 exclusions, WebGridPagerModes mode, String firstText, String previousText, String nextText, String lastText, Int32 numericLinksCount, Object htmlAttributes) +195 ASP._Page_Members_IpList_cshtml.Execute() in c:\Users\Administrator\Documents\My Web Sites\ITInvent\Members\IpList.cshtml:74 System.Web.WebPages.WebPageBase.ExecutePageHierarchy() +207 System.Web.WebPages.WebPage.ExecutePageHierarchy(IEnumerable`1 executors) +68 System.Web.WebPages.WebPage.ExecutePageHierarchy() +156 System.Web.WebPages.StartPage.RunPage() +19 System.Web.WebPages.StartPage.ExecutePageHierarchy() +65 System.Web.WebPages.WebPageBase.ExecutePageHierarchy(WebPageContext pageContext, TextWriter writer, WebPageRenderingBase startPage) +76 System.Web.WebPages.WebPageHttpHandler.ProcessRequestInternal(HttpContext context) +249Regards PMSLEET
GmGregori
Contributor
5438 Points
730 Posts
Re: Checkboxes and Select query
Mar 14, 2012 12:53 PM|LINK
Have you maybe inserted into the grid.GetHtml method's options a reference to "Mname" column?
Can you post your @grid.GetHtml statement?
PMSLEET
Member
38 Points
19 Posts
Re: Checkboxes and Select query
Mar 20, 2012 07:33 AM|LINK
Hi!
Pasting the whole page here:
@{ var db = Database.Open("dbConn"); WebGrid grid = null; if(IsPost){ var fields = new List<string>(); if(!Request["ITKontakt"].IsEmpty()){ fields.Add("ITKontakt"); } if(!Request["IP"].IsEmpty()){ fields.Add("IP"); } if(!Request["Mname"].IsEmpty()){ fields.Add("Mname"); } if(!Request["Type"].IsEmpty()){ fields.Add("Type"); } if(!Request["Appkontakt"].IsEmpty()){ fields.Add("Appkontakt"); } if(!Request["SQL"].IsEmpty()){ fields.Add("SQL"); } if(!Request["IIS"].IsEmpty()){ fields.Add("IIS"); } if(!Request["FTP"].IsEmpty()){ fields.Add("FTP"); } if(!Request["FO"].IsEmpty()){ fields.Add("FO"); } if(!Request["Beskrivning"].IsEmpty()){ fields.Add("Beskrivning"); } var IP = "%" + Request["IP"] + "%"; var data = db.Query("SELECT " + String.Join(", ",fields) + " FROM Invent WHERE IP like @0 ORDER BY IP", IP); grid = new WebGrid(source: data, defaultSort: "IP", rowsPerPage: 254); } } <!DOCTYPE html> <html> <head> <title>Lista</title> <style type="text/css"> .grid { margin: 4px; border-collapse: collapse; font-family: verdana,arial,sans-serif; font-size:12px } .head { background-color: #E0E0E0; font-weight: bold; color: #FFF; } .grid th, .grid td { border: 1px solid #C0C0C0; padding: 5px; } .alt { background-color: #E0E0E0; color: #000; } .product { width: 600px; font-family: verdana,arial,sans-serif; font-size:11px} .small { width: 150px; font-family: verdana,arial,sans-serif; font-size:11px} .font { font-family: verdana,arial,sans-serif; font-size:11px} .medium { width: 300px; font-family: verdana,arial,sans-serif; font-size:11px} </style> </head> <body> <a href="@Href("~/Members/menu")"><IMG SRC="/Images/itinvent.jpg" heigth="93" width="300" Border="0"></IMG></a><IMG SRC="/Images/netview.jpg" heigth="62" width="200"></IMG> <hr /> <h3></h3> <font face="verdana,arial,sans-serif" size="2"> <a href="InsertProducts">Lägg till post(er)</a> <br/> <a href="UpdateProducts">Uppdatera post(er)</a> <br/><br/> </font> @if(grid != null){ @grid.GetHtml(tableStyle: "grid", headerStyle: "head", alternatingRowStyle: "alt", columns: grid.Columns( grid.Column("IP", "Ip-adress:", style: "font"), grid.Column("Mname", "Maskinnamn:", style: "medium"), grid.Column("Type", "Typ:", style: "small"), grid.Column("ITKontakt","ITKontakt:", style: "small"), grid.Column("Appkontakt", "Applikationskontakt:", style: "small"), grid.Column("SQL", "SQL:", style: "font"), grid.Column("IIS", "IIS:", style: "font"), grid.Column("FTP", "FTP:", style: "font"), grid.Column("FO", "Förvaltningsobjekt:", style: "medium"), grid.Column("Beskrivning", "Beskrivning:", style: "product") ) ) } @RenderPage("/Shared/_Footer.cshtml") </body> </html>Regards PMSLEET
Mikesdotnett...
All-Star
154818 Points
19853 Posts
Moderator
MVP
Re: Checkboxes and Select query
Mar 20, 2012 07:40 AM|LINK
Check your database. The error says that there is no column called Mname in it.
Beginning ASP.NET Web Pages with WebMatrix | My Site | Twitter