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

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);
}
}

Related

How to save an username in Winforms? [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 1 year ago.
The community is reviewing whether to reopen this question as of 1 year ago.
Improve this question
I am trying to save the username entered in the "enter playername" field in order to create a highscore list, can anyone help me with that ?
namespace TikTakTo
{
public partial class Anmeldung : Form
{
public Anmeldung()
{
InitializeComponent();
}
private void button_play_Click(object sender, EventArgs e)
{
Form1.setSpielerName(Spieler_1.Text, Spieler_2.Text);
Form1 frm = new Form1();
frm.Show();
this.Hide();
}
private void Spieler_2_KeyPress(object sender, KeyPressEventArgs e)
{
if (e.KeyChar.ToString() == "\r")
button_play.PerformClick();
}
}
}
Most recommanded option for windows-forms applications is a settings file located in the Properties folder.
The data there can be saved per-user and per-application, and stored in the application's AppData folder.You can read the settings-table fields, change their values and save your changes.
e.g:Properties.Settings.Default["SomeProperty"] = "Some Value";Properties.Settings.Default.Save();
Read this question for more info.
Basically you have two options, you can either save the info to file, or save it to database. This should help you: Best way to store data locally in .NET (C#)
Always try to make your own search before asking a question!
you can write in txt file in your project folder , and after restarting the app, you can control the file in that location , if its exist you can read data and put the form.
like a cookie.
example code.
public static string _debugPath { get { return Path.GetDirectoryName(System.Reflection.Assembly.GetEntryAssembly().Location); } }
public static void WriteToText(string txt)
{
DateTime _dt = DateTime.Now;
using (StreamWriter wr = File.AppendText(Path.Combine(_debugPath, "Info.Inc")))
{
wr.WriteLine(txt+"|");
wr.Close();
}
}
public static bool ReadInfo()
{
FileInfo fi = new FileInfo(Path.Combine(_debugPath , "Info.Inc"));
if (fi.Exists)
{
using (StreamReader rd = new StreamReader(Path.Combine(_debugPath , "Info.Inc")))
{
//take data in here and put the form.
}
}
}

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.

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

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;

asp filter ajax and usual request [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 have asp webpage in Net4.0. In this version I can't use async method. I have ajax container:UpdatePanel . I have button in this panel. When I press button the page doesn't reload and in debug mode I see Page load method. I need filter ajax request and usual. How can I this do?
Like this:
protected void Page_Load(object sender, EventArgs e)
{
if(request.isAsync)
{
//this is ajax
}else
{
//this is usual request.
}
}
You can use extension method from here
public static bool IsAjaxRequest(this HttpRequest request)
{
if (request == null)
{
throw new ArgumentNullException("request");
}
return (request["X-Requested-With"] == "XMLHttpRequest") || ((request.Headers != null) && (request.Headers["X-Requested-With"] == "XMLHttpRequest"));
}
and make it:
if(request.IsAjaxRequest())

Categories