Wednesday, October 31, 2012

Linq to SQL Insert update delete read operations


  DataClasses1DataContext dbcontext = new DataClasses1DataContext();

        //insert
        protected void Button1_Click(object sender, EventArgs e)
        {

            dbcontext = new DataClasses1DataContext();
            PRDErrorCodes_ cd = new PRDErrorCodes_
                {

                    ErrorCode = "test",
                    ErrorDescription = "testDesc"

                };
            dbcontext.PRDErrorCodes_s.InsertOnSubmit(cd);
            dbcontext.SubmitChanges();
            Response.Write("successfull inserted");
        }
        //update
        protected void Button2_Click(object sender, EventArgs e)
        {
            var det = (from p in dbcontext.PRDErrorCodes_s
                       where p.ErrorCode.Equals("test")
                       select p).SingleOrDefault();

            if (det != null)
            {
                det.ErrorCode = "new test";
                det.ErrorDescription = "new test Desc";
                dbcontext.SubmitChanges();
                Response.Write("successfull saved");
            }
        }
        //delete
        protected void Button3_Click(object sender, EventArgs e)
        {
            PRDErrorCodes_ det = (from p in dbcontext.PRDErrorCodes_s
                                  where p.ErrorCode.Equals("test")
                                  select p).SingleOrDefault();

            if (det != null)
            {
                dbcontext.PRDErrorCodes_s.DeleteOnSubmit(det);
                dbcontext.SubmitChanges();
                Response.Write("deleted saved");
            }
        }
        //read
        protected void Button4_Click(object sender, EventArgs e)
        {
            var det = (from p in dbcontext.PRDErrorCodes_s
                       select p).ToList();
            GridView1.DataSource = det;
            GridView1.DataBind();
        }

Thursday, October 25, 2012

Accessing registery keye in 64 bit windows 7 machine


For information into our implicit knowledge bank.

If you on 64 bit machine and need to read registry, sometimes you need to manually copy the keys under the Wow6432Node node as shown below.




Accessing selected item from combo box wen items are added dynamically using List item


using System.Web.UI.WebControls;


//add items to combobox

 comboBox1.Items.Clear();
            List dataBundles = getDataVodacomDataBundles();
            //Value="UDB75MB">Data Bundle 75MB (R88.00)<
            foreach (sp_GetProductDataBundlesResult dataEntry in dataBundles)
            {
                string tssProductBundleValue = String.Format("{0:C0}", dataEntry.TSSProductBundleValue);
                String text = dataEntry.TSSProductBundleName + "(" + tssProductBundleValue + ")";
                String value = "" + dataEntry.TSSProductBundleMegs;
           
                ListItem li = new ListItem(text, value);
                comboBox1.Items.Add(li);
            }


string selectedVAL= ((System.Web.UI.WebControls.ListItem)(comboBox1.SelectedItem)).Value;

Wednesday, October 17, 2012

setting c# windows service in debug mode


setting service in debug mode


just comment the below lines

 ServiceBase[] ServicesToRun;
            ServicesToRun = new ServiceBase[]
            {
                new CLIMimoneyTestAsync()
            };
            ServiceBase.Run(ServicesToRun);



and write this,uncomment the above lines when, debugging is done and you want to start the service

 //CLITest service = new CLITest ();
            //service.OnDebug();
            //System.Threading.Thread.Sleep(System.Threading.Timeout.Infinite);

c# timer example


using System;
using System.Timers;

public class Timer1
{
    private static System.Timers.Timer aTimer;

    public static void Main()
    {
        // Normally, the timer is declared at the class level, 
        // so that it stays in scope as long as it is needed. 
        // If the timer is declared in a long-running method,   
        // KeepAlive must be used to prevent the JIT compiler  
        // from allowing aggressive garbage collection to occur  
        // before the method ends. You can experiment with this 
        // by commenting out the class-level declaration and  
        // uncommenting the declaration below; then uncomment 
        // the GC.KeepAlive(aTimer) at the end of the method. 
        //System.Timers.Timer aTimer; 

        // Create a timer with a ten second interval.
        aTimer = new System.Timers.Timer(10000);

        // Hook up the Elapsed event for the timer.
        aTimer.Elapsed += new ElapsedEventHandler(OnTimedEvent);

        // Set the Interval to 2 seconds (2000 milliseconds).
        aTimer.Interval = 2000;
        aTimer.Enabled = true;

        Console.WriteLine("Press the Enter key to exit the program.");
        Console.ReadLine();

        // If the timer is declared in a long-running method, use 
        // KeepAlive to prevent garbage collection from occurring 
        // before the method ends. 
        //GC.KeepAlive(aTimer);
    }

    // Specify what you want to happen when the Elapsed event is  
    // raised. 
    private static void OnTimedEvent(object source, ElapsedEventArgs e)
    {
        Console.WriteLine("The Elapsed event was raised at {0}", e.SignalTime);
    }
}

/* This code example produces output similar to the following:

Press the Enter key to exit the program.
The Elapsed event was raised at 5/20/2007 8:42:27 PM
The Elapsed event was raised at 5/20/2007 8:42:29 PM
The Elapsed event was raised at 5/20/2007 8:42:31 PM
...
 */

Tuesday, October 16, 2012

Installing c# windows service using cmd

C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil   xxxxx.exe


For EG:

"C:\Windows\Microsoft.NET\Framework\v2.0.50727\installutil"  "C:\Program Files\Virtual Payment Solutions\PRD Test\PRDTest.exe"

Thursday, October 11, 2012

FTP File Upload using FtpWebRequest in .Net C#


using System.Net;
using System.IO; 


 public void ftpfile(string ftpfilepath, string inputfilepath)
    {
        string ftphost = "127.0.0.1";
        //here correct hostname or IP of the ftp server to be given

        string ftpfullpath = "ftp://" + ftphost + ftpfilepath;
        FtpWebRequest ftp = (FtpWebRequest)FtpWebRequest.Create(ftpfullpath);
        ftp.Credentials = new NetworkCredential("userid", "password");
        //userid and password for the ftp server to given

        ftp.KeepAlive = true;
        ftp.UseBinary = true;
        ftp.Method = WebRequestMethods.Ftp.UploadFile;
        FileStream fs = File.OpenRead(inputfilepath);
        byte[] buffer = new byte[fs.Length];
        fs.Read(buffer, 0, buffer.Length);
        fs.Close();
        Stream ftpstream = ftp.GetRequestStream();
        ftpstream.Write(buffer, 0, buffer.Length);
        ftpstream.Close();
    }

Function can be used as 

ftpfile(@"/testfolder/testfile.xml", @"c:\testfile.xml");

Tuesday, October 2, 2012

Accessing Check box inside a list view c# windows



richTextBox2.Text = String.Empty;
foreach (ListViewItem item in listView1.Items)
{
    if (item.Checked)
    {
        richTextBox2.Text += item.SubItems[1].Text + Environment.NewLine;
    }
}
You could also write this with a LINQ expression and String.Join
richTextBox2.Text = String.Join(Environment.NewLine,
    from item in listView1.Items.OfType<ListViewItem>()
    where item.Checked
    select item.SubItems[1].Text);


http://stackoverflow.com/questions/10908416/how-to-grab-subitems-with-checkboxes-in-listview




private bool allowCheck = false;
private bool preventOverflow = true;
private void lstvwRaiseLimitStore_MouseClick(object sender, MouseEventArgs e)
    {
        preventOverflow = false;
        ListViewItem item = lstvwRaiseLimitStore.HitTest(e.X, e.Y).Item;
        if (item.Checked)
        {
            allowCheck = true;
            item.Checked = false;
        }
        else
        {
            allowCheck = true;
            item.Checked = true;
        }
    }
private void lstvwRaiseLimitStore_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        if (!preventOverflow)
        {
            if (!allowCheck)
            {
                preventOverflow = true;
                e.Item.Checked = !e.Item.Checked;
            }
            else
                allowCheck = false;
        }
    }
OR
private bool allowCheck = false;
private bool preventOverflow = true;

private void lstvwRaiseLimitStore_MouseClick(object sender, MouseEventArgs e)
    {
        preventOverflow = false;
        ListViewItem item = lstvwRaiseLimitStore.HitTest(e.X, e.Y).Item;
        if (item.Checked)
        {
            allowCheck = true;
            item.Checked = false;
        }
        else
        {
            allowCheck = true;
            item.Checked = true;
        }
    }

private void lstvwRaiseLimitStore_ItemChecked(object sender, ItemCheckedEventArgs e)
    {
        if (!preventOverflow)
        {
            if (!allowCheck)
            {
                preventOverflow = true;
                e.Item.Checked = !e.Item.Checked;
            }
            else
                allowCheck = false;
        }
    }