Right Approach to Make cart in asp.net [closed] - c#

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I'm making a shopping cart in asp.net with c# using DataTable stored in Session and all items stored in DataTable.
public DataTable dtCart = new DataTable();
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
if (Session["dtCart"] == null) { CreateCartTable(); }
}
}
protected void CreateCartTable()
{
dtCart.Rows.Clear();
dtCart.Columns.Clear();
dtCart.Columns.Add(new DataColumn("Prd_Id", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Prd_Name", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Prd_Img", Type.GetType("System.String")));
dtCart.Columns.Add(new DataColumn("Qty", Type.GetType("System.Int32")));
dtCart.Columns.Add(new DataColumn("Rate", Type.GetType("System.Decimal")));
dtCart.Columns.Add(new DataColumn("Amount", Type.GetType("System.Decimal")));
dtCart.Columns.Add(new DataColumn("TotalAmount", Type.GetType("System.Decimal")));
Session["dtCart"] = dtCart;
}
Is this right approach or any other way is more elegant and light on application?
FYI I'm using web forms.

You can make a class to cart:
public class Cart
{
public string Prd_Id{set;get;}
public string Prd_Name{set;get;}
//and so on...
}
then in page:
Cart obj=new Cart{Prd_Id="1",Prd_Name="John"};
Session["dtCart"]=obj;

Related

No overload for method '' takes 0 arguments [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 4 years ago.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Improve this question
I have a problem with the code in rec.Speechreconized += rec_Speachrecognized.
I have been looking for answers in the internet but it just won't work. I hope someone can help me.
namespace ai
{
public partial class Form1 : Form
{
SpeechSynthesizer s = new SpeechSynthesizer();
Choices list = new Choice {};
public Form1()
{
SpeechRecognitionEngine rec = new SpeechRecognitionEngine();
list.Add(new String[] {"Hello", "how are you"});
Grammar gr = new Grammar(new GrammarBuilder(list));
try
{
rec.RequestRecognizerUpdate();
rec.LoadGrammar(gr);
rec.SpeechRecognized += rec_Speachrecognized();
rec.SetInputToDefaultAudioDevice();
rec.RecognizeAsync(RecognizeMode.Multiple);
}
catch{return;}
s.Speak("Hi, I am Ms M, what can i help you?");
InitializeComponent();
}
public void Say(String h)
{
s.Speak(h);
}
private EventHandler<SpeechRecognizedEventArgs> rec_Speachrecognized(object sender, SpeechRecognizedEventArgs e)
{
string r = e.Result.Text;
if(r == "hello")
{
Say("hi");
}
throw new NotImplementedException();
}
private void Form1_Load(object sender, EventArgs e)
{
}
}
}
you need to change this line
rec.SpeechRecognized += rec_Speachrecognized();
to
rec.SpeechRecognized += rec_Speachrecognized;
basically remove the () at the end since the event will pass the params but this way you are calling the method without params

trying to do something super cool on my web site [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
how to do that if user did not did anything on the website for a set amount of time the website will log out him?
is it from the c# page?
how can i tocount the time sincethe last touch on something?
and how can i alert it?
plss help
i want to add it here:
protected void Page_Load(object sender, EventArgs e)
{
if (Session["Conect"]!=null)
{
if (!(bool)Session["Conect"])
{
Response.Redirect("Must-Had.aspx", true);
}
}
else
Response.Redirect("Must-Had.aspx", true);
if (Session["val"]!=null)
{
if ((bool)Session["val"])
{
limission4.Visible = true;
mission4.InnerText =(string)Session["tit"];
}
}
if (Session["val2"] != null)
{
if ((bool)Session["val2"])
{
limission5.Visible = true;
mission5.InnerText = (string)Session["tit2"];
}
}
}
Place this somewhere between the <body> and </body> tags.
<script type="text/javascript">
var timeoutHandle = window.setTimeout("window.close();", 3000);
window.onscroll = function (e) {
window.clearTimeout(timeoutHandle);
timeoutHandle = window.setTimeout("window.close();", 3000);
}
</script>
You can change the "3000" to whatever amount of time you want the page to last.
You can also change the "window.close();" to whatever you want the page to do when the time runs out. The "window.close();" command will just close the page.

C# Global Object [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
I am building a program for my course in which i need global objects as i intend to have the object accessible from many forms and edited also. Before saying just use global variables i cant the specs state for use of OOP.
my latest attempt to fix this is using a public class but this gave a protection error problem
Code:
Form1.cs (forgot to rename and not re doing all the code and design)
public class ObjectsGlobal
{
Bays bay1 = new Bays();
Bays bay10 = new Bays();
}
frmInput.cs
private void btnAdd_Click(object sender, EventArgs e)
{
if ( 1 == Convert.ToInt32(nudBayNum))
{
ObjectsGlobal.bay1.CarMake = txtMake.Text;
}
}
any ideas are welcome at this point
Change it to:
public static class ObjectsGlobal
{
public static Bays bay1 = new Bays();
public static bay10 = new Bays();
}
Also, as recommended in a comment I have now read, take a look at the Singleton Pattern.

Can't add anything in my DB C# using ADO Entity [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I'm a beginner on Visual Studio so, please, be gentle :)
Here the code i'm using :
private void button1_Click(object sender, EventArgs e)
{
using (KrqcSDBEntities context = new KrqcSDBEntities())
{
activite monActivite = new activite();
monActivite.lieu_activite = "Paris";
monActivite.nom_activite = "Natation";
context.activite.AddObject(monActivite);
activite test = context.activite.FirstOrDefault(o => o.id == 1);
context.SaveChanges();
}
}
I succesfully succeed to get value from my DB but i can't add anything in it without any error.
Can you please have a look on it and try to help me ?
It would be very nice !
Thanks you
SaveChanges first, then get the item from data store:
private void button1_Click(object sender, EventArgs e)
{
using (KrqcSDBEntities context = new KrqcSDBEntities())
{
activite monActivite = new activite();
monActivite.lieu_activite = "Paris";
monActivite.nom_activite = "Natation";
context.activite.AddObject(monActivite);
context.SaveChanges();
activite test = context.activite.FirstOrDefault(o => o.id == 1);
}
}

C# read and calculate multiple values from textbox [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I have a question about creating calculator in C# Windows Form Application.
I want it to be possible write with form buttons an expression in textbox so for example 2+3+7= and after pressing "=" button program will read all digits and signs and perform calculation... I don't know from where to start and how could do it in such a way. Any help to any reference or smth to look at how to start doing such a expressions?
Main thing is how to read, seperate and after calculate values from textbox.
Thanks.
With the Split method you could solve this rather easy.
Try this:
private void button1_Click(object sender, EventArgs e)
{
string[] parts = textBox1.Text.Split('+');
int intSum = 0;
foreach (string item in parts)
{
intSum = intSum + Convert.ToInt32(item);
}
textBox2.Text = intSum.ToString();
}
If you would like to have a more generic calculation, you should look at this post:
In C# is there an eval function?
Where this code snippet would do the thing:
public static double Evaluate(string expression)
{
System.Data.DataTable table = new System.Data.DataTable();
table.Columns.Add("expression", string.Empty.GetType(), expression);
System.Data.DataRow row = table.NewRow();
table.Rows.Add(row);
return double.Parse((string)row["expression"]);
}
private void button1_Click(object sender, EventArgs e)
{
textBox2.Text = Evaluate(textBox1.Text).ToString();
}

Categories