Basically I am making a webform where you will fill all of the textboxes out and then select a category from a dropdown and hit submit. Based upon which category you select should dictate what string the data from the textboxes is stored in. I'm on a novice level when it comes to C# and ASP.NET and something is off about my if statements but I can't figure out how to do them properly.
Code below:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
public partial class _Default : System.Web.UI.Page
{
string non_fiction;
string fiction;
string self_help;
protected void Page_Load(object sender, EventArgs e)
{
}
protected void Submit_btn_Click(object sender, EventArgs e)
{
if (Cat_DropDownList.SelectedIndex = 0)
{
fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
if (Cat_DropDownList.SelectedIndex = 1)
{
non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
if (Cat_DropDownList.SelectedIndex = 2)
{
self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
}
}
Also to save another post I need to figure out a way to have these stored so I can call the "full" strings and add them to another one on another page.
I would declare
StringBuilder non_fiction = new StringBuilder();
StringBuilder fiction = new StringBuilder();
StringBuilder self_help = new StringBuilder();
StringBuilder[] strings = null;
and use them as
protected void Page_Load(object sender, EventArgs e)
{
strings = new StringBuilder[] { fiction, non_fiction, self_help };
}
protected void Submit_btn_Click(object sender, EventArgs e)
{
strings[Cat_DropDownList.SelectedIndex].Append("Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text);
}
without ifs and switches
first you are missing == operator in if condition . you need to use == operator for comaparision
if (Cat_DropDownList.SelectedIndex == 0)
{
fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
if (Cat_DropDownList.SelectedIndex == 1)
{
non_fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
if (Cat_DropDownList.SelectedIndex == 2)
{
self_help = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
if (Cat_DropDownList.SelectedIndex = 0)
{
fiction = "Title: " + Titletxt.Text + " | " + "Description: " + Descriptiontxt.Text + " | " + "Price: " + Pricetxt.Text + " | " + "Quantity: " + Quantitytxt.Text;
}
= is assignment - you want a comparison with == instead - same applies to the other if statements. Also using string.Format() would make this statement much more readable (imo):
if (Cat_DropDownList.SelectedIndex == 0)
{
fiction = string.Format("Title: {0} | Description: {1} | Price: {2} | Quantity: {3}", Titletxt.Text, Descriptiontxt.Text, Pricetxt.Text, Quantitytxt.Text);
}
Related
How can I show the Debug.Log in 2 lines (like adding an enter or <br>) in C#?
I have lots of info to show and compare, yet it is not convenient to read it in one line.
Current result: Debug.Log("Pointer:" + " " + pointerPosX + " " + pointerPosY + " " + "target" + " " + targetPosX + " " + targetPosY);
Expected result:
Pointer: pointerPosX + " " + pointerPosY
Target: targetPosX " " " + targetPosY
P.S. This is my first question in StackOverflow - so, please, let me know if I did something wrong.
Option 1 (\n):
Debug.Log("Pointer:" + " " + pointerPosX + " "
+ pointerPosY + "\n" + "Target:" + " " + targetPosX + " " + targetPosY);
Option 2 (System.Environment.NewLine):
Debug.Log("Pointer:" + " " + pointerPosX + " " + pointerPosY +
System.Environment.NewLine + "Target:" + " " + targetPosX + " " + targetPosY);
Using $ - string interpolation
Option 1:
Debug.Log($"Pointer: {pointerPosX} {pointerPosY}\nTarget: {targetPosX} {targetPosY}");
Option 2:
Debug.Log($"Pointer: {pointerPosX} {pointerPosY}{System.Environment.NewLine}Target: {targetPosX} {targetPosY}");
Difference between "\n" and Environment.NewLine
protected void Button1_Click(object sender, EventArgs e)
{
result.Text = "Hello! Here is your school information:" <br />
"Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
"Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
"Address: " + add.Text +
"Age: " + age.Text +
"Contact Information: " + num.Text + " / " + email.Text + "";
}
How can I add a newline here? For example;
Full Name: Bill Gates
Course, Year and Section:
Just like that format
I usually use Environment.NewLine, or you can add string "\r\n" or "\n" (it depends on system). More here
Edit:
As mentioned in comments, if it is web page output, html tag for new line is <br>. But for C# it is just string, so your code should be
protected void Button1_Click(object sender, EventArgs e)
{
result.Text = "Hello! Here is your school information:" + "<br />" +
"Full Name: " + fname.Text + " " + mname.Text + " " + lname.Text +
"Course, Year and Section: " + cour.Text + " " + yr.Text + "" + sec.Text +
"Address: " + add.Text +
"Age: " + age.Text +
"Contact Information: " + num.Text + " / " + email.Text + "";
}
i would like to make each element of textblockresult visible only when the value is not null.
Intent, ROKEntity, ProcedureName etc are strings that are defined as empty and then receive their value from a json :
string intent = "";
if (!string.IsNullOrEmpty(root.XPathSelectElement("//intent").Value))
{
intent = root.XPathSelectElement("//intent").Value;
}
resultToDisplay = "Action: " + intent
+ Environment.NewLine + "Rok Entity: " + ROKEntity
+ Environment.NewLine + "Rok Entity: " + ROKEntity2
+ Environment.NewLine + "Rok Entity: " + ROKEntity3
+ Environment.NewLine + "Procedure Name: " + ProcedureName
+ Environment.NewLine + "Folder Name: " + FolderName
+ Environment.NewLine + "Task Name: " + TaskName
+ Environment.NewLine + "Worker Name: " + WorkerName
+ Environment.NewLine + "Risk Name: " + RiskName
+ Environment.NewLine + "File Name: " + FileName
+ Environment.NewLine + "Workflow Name: " + WorkflowName
+ Environment.NewLine + "Model Name: " + ModelName
+ Environment.NewLine + "Position Name: " + PositionName
+ Environment.NewLine + "Created by: " + CreatedBy
+ Environment.NewLine + "Modified by: " + ModifiedBy
+ Environment.NewLine + "From: " + From
+ Environment.NewLine + "To: " + To
+ Environment.NewLine + "Display Mode: " + DisplayMode
+ Environment.NewLine + "Data Tracking: " + DataTracking
+ Environment.NewLine + "Details: " + Details
+ Environment.NewLine + "Export Format: " + ExportFormat
+ Environment.NewLine + "Ged Files: " + GedFiles
+ Environment.NewLine + "Ged Folders: " + GedFolders
+ Environment.NewLine + "Map: " + Map
+ Environment.NewLine + "Kpi: " + Kpi
+ Environment.NewLine + "Procedure Type: " + ProcedureType
TextBlockResult.Text = resultToDisplay;
ie i want "Action: " + intent to be visible only when the value of intent is not null nor empty, the same for "Rok Entity: " + ROKEntity and so forth ..
For the moment, i have the following xaml that only allows me to set the visibility of the whole lot to collapsed or hidden or visible:
<TextBlock x:Name="TextBlockResult" Visibility="Collapsed" TextWrapping="Wrap" TextAlignment="Center" FontFamily="Segoe UI" FontSize="16" Foreground="Black"><Run Text="Result"/><InlineUIContainer>
What are the different steps to achieve that please ?
if I understand correctly what you are trying to do, you can just skip the elements with empty or null value. So have a method like
private void AddToDisplay(StringBuilder sb, string title, string value)
{
if (!string.IsNullOrEmpty(value))
{
if (sb.Length > 0)
sb.Append(Environment.NewLine);
sb.Append(title);
sb.Append(value);
}
}
then build your result like
var sb = new StringBuilder();
AddToDisplay(sb, "Action: ", intent);
AddToDisplay(sb, "Rok Entity: ", ROKEntity);
.
.
resultToDisplay = sb.ToString();
that way your results show only non-null non-empty elements and optionally hide the entire TextBlock if resultToDisplay is empty after you are done building it
This question already has answers here:
LINQ to Entities does not recognize the method
(5 answers)
Closed 6 years ago.
I have this LINQ query, I have problem with its Aggregate part:
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
And I get this exception.
{"LINQ to Entities does not recognize the method 'System.String
Aggregate[String](System.Collections.Generic.IEnumerable1[System.String],
System.Func3[System.String,System.String,System.String])' method, and
this method cannot be translated into a store expression."}
Why am I getting this exception? How can I get rid of it? Thanks.
Aggregate has not translation to SQL, so Enumerate the results to memory:
.AsEnumerable().Aggregate((current, next) => current + " | " + next);
OR:
.ToList().Aggregate((current, next) => current + " | " + next);
The problem is that the .Aggregate() call is part of the projection to some type, and adding a .ToList() call inside the projection won't work since that projection is translated to sql as a whole. You cannot tell EF to translate half of the projection to SQL, and the other half not. You have to split the projection in two parts, and tell EF to translate the first part, but not the second.
Ok, to solve this. At the moment you have something like this. I can't be specific, and it will be different from your query, since you do not show the full query.
var result = ctx.SomeTable
.Where(...whatever...)
.Select(x => new SomeEntity {
// lots of properties
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | "
m.RelatedMultipleWorks.Select(z => z.RelatedCity + " + " + z.RelatedCounty + " + " +
z.RelatedDistrict + " + " + z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad)
.Aggregate((current, next) => current + " | " + next),
// other properties
})
.ToList();
To eliminate the .Aggregate() call in the projection, you need to leave the datasource of the Aggregate intact so L2E can take that data from the database. After that you can apply the .Aggregate() in-memory.
var result = ctx.SomeTable
.Where(...whatever...)
// Change: Do not project to an entity, but to an anonymous type
.Select(x => new {
// lots of properties
// Change: Removed the Aggregate-part
Adresses = m.RelatedMultipleWorks.Count == 0 ?
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + " + " + m.Road :
m.RelatedCity + " + " + m.RelatedCounty + " + " + m.RelatedDistrict + " + " +
m.RelatedNeighborhood + " + " + m.RelatedStreet + m.RelatedRoad + " | ",
// Added: New property for the aggregate-datasource
RelatedAdresses = m.RelatedMultipleWorks
.Select(z =>
z.RelatedCity + " + " + z.RelatedCounty + " + " + z.RelatedDistrict + " + " +
z.RelatedNeighborhood + " + " + z.RelatedStreet + " + " + z.RelatedRoad
)
// other properties
})
// Added: Call AsEnumerable to stop translating to SQL
.AsEnumerable()
// Added: Project again, fairly simple since all properties are already ok, but now call the Aggregate
.Select(x => new SomeEntity {
// lots of properties
Adresses = Adresses + RelatedAdresses.Aggregate((current, next) => current + " | " + next)
// other properties
})
.ToList();
I want to write that two strbldrSQL.Append in single strbldrSQL.
Because Im inserting records in 2 DataSets dsorderHeader and dsorderDetail respectively.
if (strbldrSQL != null) strbldrSQL.Clear();
else strbldrSQL = new StringBuilder();
strbldrSQL.Append(#" SELECT ORDHEDR01.* FROM ORDHEDR01 "
+ " WHERE ORDHEDR01.COMPANY = " + argintCompany + " "
+ " AND ORDHEDR01.WHSE = " + argintWarehouse + " "
+ " AND ORDHEDR01.ORDNUM = " + arglngOrderNumber + " "
);
dsOrderHeader = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderHeader == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";
strbldrSQL.Clear();
strbldrSQL.Append(#" SELECT ORDDETL02.* FROM ORDDETL02 "
+ " WHERE ORDDETL02.COMPANY = " + argintCompany + " "
+ " AND ORDDETL02.WHSE = " + argintWarehouse + " "
+ " AND ORDDETL02.ORDNUM = " + lngOrderNumber + " "
);
dsOrderDetail = DAL.pfGetDataSet(ref argobjMnCnnCls, strbldrSQL.ToString(), true);
if (dsOrderDetail == null) return CNS.gcUpdateFailed + ";" + "Unable to get data";