Insert, Update, Delete query using LINQ in datagridview in C# Windows Programming? - linq

Scenario: There is following controls in the form:
datagridview, textbox1, textbox2, button(save,edit,update,delete)
1.By clicking on save button, data should be updated into datagridview at run-time.
2.By selecting complete row and clicking on edit button, data should be retrieved into textboxes.
3.By clicking on Update button, that data should be updated.
4.By selecting a complete row, the row should be deleted.

This is my code.
I hope it will help you
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
namespace LINQ_Test
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
LINQtestDataContext dc = new LINQtestDataContext();
public void show_data()
{
dataGridView1.DataSource = (from t in dc.LinqTests
select t);
}
public void insert_data()
{
try
{
LinqTest tbl = new LinqTest
{
ID=Convert.ToInt32(textBox_id.Text),
Name=textBox_name.Text
};
dc.LinqTests.InsertOnSubmit(tbl);
dc.SubmitChanges();
MessageBox.Show("Data Inserted!!!");
show_data();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void update_data()
{
try
{
LinqTest tbl = dc.LinqTests.Single(x=>x.ID==Convert.ToInt32(textBox_id.Text));
tbl.Name = textBox_new_name.Text;
dc.SubmitChanges();
MessageBox.Show("Data Updated!!!");
show_data();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
public void delete_data()
{
try
{
LinqTest tbl = dc.LinqTests.Single(x => x.ID == Convert.ToInt32(textBox_id.Text));
dc.LinqTests.DeleteOnSubmit(tbl);
dc.SubmitChanges();
MessageBox.Show("Data Deleted!!!");
show_data();
}
catch (Exception ex)
{
MessageBox.Show(ex.Message);
}
}
private void Form1_Load(object sender, EventArgs e)
{
show_data();
}
private void button_insert_Click(object sender, EventArgs e)
{
insert_data();
}
private void button_update_Click(object sender, EventArgs e)
{
update_data();
}
private void button_delete_Click(object sender, EventArgs e)
{
delete_data();
}
}
}

Related

How to Supress / permently Hide error box of windows media player

HI i wanted to Hide this error box:
i tried serching on internet but did not find usefull results , i am a fresher in coding so any way or any code that can help me ?
This issue is caused from server not acsepting first time but secind time it works great .
it would be great if u provide me easy steps .
MY Currrent code is
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
namespace Media_Player
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void button1_Click(object sender, EventArgs e)
{
}
private void axWindowsMediaPlayer1_Enter(object sender, EventArgs e)
{
label2.AutoSize = true;
label1.AutoSize = true;
}
private void button1_Click_1(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = ("http://y0b.net/radiosa.m3u");
MessageBox.Show("Successfuly Selected Radio SA , you will encounter a error , click close and then click play . " , "Thank You" , MessageBoxButtons.OK);
axWindowsMediaPlayer1.Ctlcontrols.play();
label2.Text = "Playing Radio SA";
}
private void button2_Click_1(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.stop();
}
private void button3_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
}
private void button4_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = ("http://y0b.net/radiosa2.m3u");
MessageBox.Show("Successfuly Selected Radio SA Clasic , you will encounter a error after this mesage , click close and then click play . ", "Thank You", MessageBoxButtons.OK);
axWindowsMediaPlayer1.Ctlcontrols.play();
label2.Text = "Playing Radio SA CLASSIC ";
}
private void button5_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
private void Form1_Load(object sender, EventArgs e)
{
}
private void radioButton1_CheckedChanged(object sender, EventArgs e)
{
axWindowsMediaPlayer1.Visible = false;
}
private void radioButton2_CheckedChanged(object sender, EventArgs e)
{
radioButton1.Checked = false;
axWindowsMediaPlayer1.Visible = true;
}
private void button6_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.URL = ("http://y0b.net/radiosa3.m3u");
MessageBox.Show("Successfuly Selected Radio SA Dance Department , you will encounter a error after this mesage , click close and then click play . ", "Thank You", MessageBoxButtons.OK);
axWindowsMediaPlayer1.Ctlcontrols.play();
label2.Text = "Playing Radio SA Dance Department";
}
private void trackBar1_Scroll(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.volume = trackBar1.Value;
label4.Text = trackBar1.Value.ToString();
}
private void label4_Click(object sender, EventArgs e)
{
}
private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
}
private void button7_Click(object sender, EventArgs e)
{
}
private void progressBar1_Click(object sender, EventArgs e)
{
axWindowsMediaPlayer1.settings.playCount.ToString();
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
axWindowsMediaPlayer1.Ctlcontrols.pause();
}
}
}
Try this out. I suppressed the error message by setting enableErrorDialogs to false, then repeatedly asked it to play every 250 ms in a loop, with a ten second time out:
private async void button1_Click(object sender, EventArgs e)
{
button1.Enabled = false;
axWindowsMediaPlayer1.settings.autoStart = true;
axWindowsMediaPlayer1.settings.enableErrorDialogs = false;
axWindowsMediaPlayer1.URL = "http://y0b.net/radiosa3.m3u";
DateTime stopAt = DateTime.Now.AddSeconds(10);
while (DateTime.Now<stopAt && axWindowsMediaPlayer1.playState!=WMPLib.WMPPlayState.wmppsPlaying)
{
axWindowsMediaPlayer1.Ctlcontrols.play();
await Task.Delay(250);
}
if (axWindowsMediaPlayer1.playState != WMPLib.WMPPlayState.wmppsPlaying)
{
MessageBox.Show("Failed to load stream!");
}
button1.Enabled = true;
}
Music started playing after about two seconds for me. Your mileage may vary...
Note that I added async to the method handler at the top to allow the use of await Task.Delay(250);.

C# MySql - data does not appear after insert new data

First of all, I'm new to C# and also programming. So, my situation is after I insert data from a textbox to MySql database, the new data does not appear. FYI, I have the navigation (next previous) button. I have to re-run the apps in order for the new data to be displayed. Here's part of the codes:
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//MySql codes to display in textbox
showData(pos);
}
public void showData(int index)
{
txtDisplay.Text = table.Rows[index][0].ToString();
}
private void btnPrevious_Click(object sender, EventArgs e)
{
}
private void btnNext_Click(object sender, EventArgs e)
{
}
private void btnGenNext_Click(object sender, EventArgs e)
{
//codes to generate new data and also insert into db
}
}
Here's my full codes:

Fault Exception windows phone connecting to WCF WP8

I have created a WCF which I then created a WP8 application and added the service reference. The error is "An exception of type 'System.ServiceModel.FaultException' occurred in System.ServiceModel.ni.dll but was not handled in user code"
If anyone could possible give me some indication as I have googled this and found nothing the same.
The error comes up here
public void EndAddUser(System.IAsyncResult result)
{
object[] _args = new object[0];
base.EndInvoke("AddUser", _args, result);
}
My main classes:
public partial class MainPage : PhoneApplicationPage
{
private Service1Client _serviceClient;
// Constructor
public MainPage()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.LoginUserCompleted += new
}
private void loginBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.LoginUserAsync(userNameTxtBox.Text, passwordTxtBox.Text);
}
private void newAccountBtn_Click(object sender, RoutedEventArgs e)
{
this.NavigationService.Navigate(new Uri("/AddAccount.xaml", UriKind.Relative));
}
private void _serviceClient_LoginUserCompleted(object senter,
LoginUserCompletedEventArgs e)
{
if (e.Error == null && e.Result != null)
{
MessageBox.Show("Welcome " + e.Result + "!");
}
else
{
MessageBox.Show("Could not log in. Please check user name/password and try
again.");
}
}
}
public partial class AddAccount : PhoneApplicationPage
{
private Service1Client _serviceClient;
public AddAccount()
{
InitializeComponent();
_serviceClient = new Service1Client();
_serviceClient.AddUserCompleted += new EventHandler<AsyncCompletedEventArgs>
(_serviceClient_AddUserCompleted);
}
private void addAccountBtn_Click(object sender, RoutedEventArgs e)
{
_serviceClient.AddUserAsync(fullnameTxtBox.Text, userNameTxtBox.Text,
passwordTxtBox.Text);
}
void _serviceClient_AddUserCompleted(object sender, AsyncCompletedEventArgs e)
{
if (e.Error == null)
{
MessageBox.Show("User account created. Please login");
this.NavigationService.GoBack();
}
else
{
MessageBox.Show("User account could not be added, Please try again");
}
}
}

How to make back button return to the system Windows Phone

I have a small app there is 3 seconds intro page, then the content page. When I push back button I go back to the intro screen, but I think I should go back to the system. How to do it?
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using System.ServiceModel.Syndication;
using System.Xml;
using Microsoft.Phone.Tasks;
namespace RSS {
public partial class FeedPage : PhoneApplicationPage {
public FeedPage() {
InitializeComponent();
this.Loaded += new RoutedEventHandler(PhonePage1_Loaded);
}
protected override void OnNavigatedTo(System.Windows.Navigation.NavigationEventArgs e) {
clearBackStack();
base.OnNavigatedTo(e);
}
void clearBackStack() {
while (this.NavigationService.BackStack.Any()) {
this.NavigationService.RemoveBackEntry();
}
}
void PhonePage1_Loaded(object sender, RoutedEventArgs e) {
// clearBackStack();
WebClient wc = new WebClient();
wc.OpenReadCompleted += new OpenReadCompletedEventHandler(wc_OpenReadCompleted);
wc.OpenReadAsync(new Uri("http://www.carmagazine.co.uk/Shared/Handlers/RssHandler.ashx?&N=190&Ns=P_Publication_Date|1&?"));
}
void wc_OpenReadCompleted(object sender, OpenReadCompletedEventArgs e) {
SyndicationFeed feed;
try {
using (XmlReader reader = XmlReader.Create(e.Result)) {
feed = SyndicationFeed.Load(reader);
lista.ItemsSource = feed.Items;
}
} catch (WebException we) { MessageBox.Show("Internet connection is down.");}
}
private void Button_Click(object sender, System.Windows.RoutedEventArgs e) {
WebBrowserTask webBrowserTask = new WebBrowserTask();
String url = (String)((Button)sender).Tag;
webBrowserTask.Uri = new Uri(url);
webBrowserTask.Show();
}
}
}
You should clear the BackStack in the OnNavigateTo method of your content page
while (this.NavigationService.BackStack.Any())
{
this.NavigationService.RemoveBackEntry();
}
The following code is the best practice for the back button key press.
protected override void OnBackKeyPress(CancelEventArgs e)
{
while (NavigationService.CanGoBack)
NavigationService.RemoveBackEntry();
base.OnBackKeyPress(e);
}
This ensures that your application will exit and return to the main screen on pressing the BackKey.

Detect Autoscrollposition value change in panel

How to detect if the Autoscrollposition value changes in the panel1?
For example,
textbox1 and textbox2.
which added in panel1. The autoscroll property is set to true.
I am only interested in detecting when the value of panel autoscrollposition changes.
The above for dynamic textboxes which are incremented.
Software in use: C#, Visual Studio 2005.
The Component required for it. is:
ListBox1
ListBox2
Panel
Button.
The Namespace for Class:
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Collections;
Here is the Complete Solution Code:
namespace detectpanelvalue
{
public partial class Form1 : Form
{
private Point tbpoint = new Point(10, 14);
private Point tbbpoint = new Point(300, 14);
private ArrayList arylst;
private ArrayList arylst1;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
panel1.Paint += new PaintEventHandler(panel1_Paint);
}
void panel1_Paint(object sender, PaintEventArgs e)
{
System.Drawing.Point pnlpt;
pnlpt = panel1.AutoScrollPosition;
if (tbpoint !=null || pnlpt != null )
{
pnlpt = tbpoint;
}
arylst1 = new ArrayList();
arylst1.Add(pnlpt);
}
private void runtime()
{
foreach (Point pt in arylst)
{
listBox1.Items.Add(pt);
}
foreach (Point ptt in arylst1)
{
listBox2.Items.Add(ptt);
}
}
private void button1_Click(object sender, EventArgs e)
{
TextBox tb = new TextBox();
tb.Location = tbpoint;
this.panel1.Controls.Add(tb);
tbpoint.Y += 30;
TextBox bb = new TextBox();
bb.Location = tbbpoint;
this.panel1.Controls.Add(bb);
tbbpoint.Y += 30;
arylst = new ArrayList();
arylst.Add(tbpoint);
runtime();
}
}
}
It is helpful for adjust the panel autoscrollposition.

Resources