Tuesday, October 30, 2018

Page life cycle of MVC

Below are the processed followed in the sequence -
→App initialization
→Routing
→Instantiate and execute controller
→Locate and invoke controller action
→Instantiate and render view.

Thursday, October 11, 2018

EF 6 Code-First Conventions

 Conventions  are the default rules which automatically configure a conceptual model.

Tuesday, October 9, 2018

Create Connection With Store Procedure with MySql in c#

MySqlConnection con = new MySqlConnection("server=YourServerName;port=3306;
database=yourdbname;uid=youruid;pwd=your PWD;");
string firstparanamevalue="Your Value";
string secondparanamevalue="Your Value";
MySqlCommand cmd1 = new MySqlCommand();
cmd1 = con.CreateCommand();
cmd1.CommandText = "YourProcedureName";
cmd1.CommandType = CommandType.StoredProcedure;
cmd1.Parameters.AddWithValue("firstparaname", firstparanamevalue);
cmd1.Parameters.AddWithValue("secondparaname", secondparanamevalue);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();


If Any Problem Please Comment.

Confirmation in Window Form in c#

var confirmResult = MessageBox.Show("Click 'Yes' To Close or 'No' to Cancel ??","Confirm To Close!!",MessageBoxButtons.YesNo);

            if (confirmResult == DialogResult.Yes)
            {
              //Your Code
            }

Action Filter in MVC

Whenever A user request is routed to a controller there may be a need to execute some logic before or after execution of particular operation ,For Achieving this ASP .Net provides functionality Called Action Filter.

Types of Filters

1. Action Filter :

      Action filter is a logic which execute after and before a controller action executes.

2. Authorization Filter :

       Authorization filter is used to implement authentication and  Authorization on controller action.

3. Result Filter :

       Result Filter is used to execute after and before view result is executed.

4. Exception Filter

        Exception Filter are used to handle errors raised by your controller and controller result.

Wednesday, September 19, 2018

Autocmplete Text Box in mvc using entity framework with some extra information.


On View:

<link rel="stylesheet" href="//code.jquery.com/ui/1.11.4/themes/start/jquery-ui.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.4/jquery-ui.js"></script>
<script type="text/javascript">
    $(document).ready(function () {
        $("#customer").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "/AppointmentList/customerlist",
                    type: "POST",
                    dataType: "json",
                    data: { searchtxt: request.term },
                    success: function (data) {
                        response($.map(data, function (item) {
                            return { label: item.Name, value: item.Name, id: item.CustomerID };
                           
                        }))

                    }
                })
            },
            messages: {
                noResults: "",
                results: function (count) {
                    return count + (count > 1 ? ' results' : ' result ') + ' found';
                }
            },
            select: function (e, i) {
                $("#ContactPerson").val(i.item.id);
            }
        });
    })
</script>

On Controller:

 [HttpPost]


        public JsonResult customerlist(string searchtxt)
       {
            var customerlist = crmapp.customers.Where(x => x.Name.StartsWith(searchtxt.ToLower()));
            return Json(customerlist, JsonRequestBehavior.AllowGet);
        }

Monday, September 17, 2018

Swap two Number without using 3rd variable in c#:

            int a = 1, b = 2;
            Console.WriteLine("Before Swapping :"+a + "," + b);
            a = a + b;
            b = a - b;
            a = a - b;
            Console.WriteLine("After Swapping :" + a + "," + b);

Swap neighbor character in string in c#:

            string str = "TAPAN";
            Console.WriteLine("Before Swaping :"+str);
            string str1 = "";
            Console.Write("After Swaping :");
            for (int i = 0; i < str.Length; i = i + 2)
            {
                int j = 0;
                if (str.Length % 2 != 0 && i == str.Length - 1) { j = 1; }
                str1 += j == 0 ? str[i + 1].ToString() : "";
                str1 += str[i].ToString();
            }
            Console.WriteLine(str1);

check number is Prime or not Prime in c#.

            int number = 13;
            int pr = 0;
            for (int i = 2; i < number; i++)
            {
                if (number % i == 0)
                {
                    pr++;
                }
            }
            if (pr > 0)
            {
                Console.WriteLine("No is not Prime");
            }
            else
            {
                Console.WriteLine("No is Prime");
            }

Fibonacci series in c# :

            int kk = 1, jj = 0, yy = 0;
            Console.Write(yy);
            for (int i = 0; i < 10; i++)
            {
                jj = kk;
                kk = yy;
                yy = kk + jj;
                Console.Write("," + yy);
            }

Factorial Of Given Number in c#:

            int factnumber = 6;
            int fact = 1;
            for (int i = factnumber; i > 0; i--)
            {
                fact = fact * i;
            }
            Console.WriteLine("\nFact :" + fact);

1. Sum OF Given number c#.

2. Reverse  a number c#.

3. Count 1 in given number c#.

string getnumber = "";
            int sum = 0;
            Console.Write("Enter No :");
            getnumber = Console.ReadLine();
            int one = 0;
            for (int i = 0; i < getnumber.Length; i++)
            {
                string s = getnumber[i].ToString();
                if (s == "1")
                {
                    one++;
                }
                sum = sum + Convert.ToInt32(s);
            }

            Console.WriteLine("Sum Of Digits Of No :"+sum);
            Console.WriteLine("No of One :" + one);
            Console.Write("Reverse No.");
            for (int i = getnumber.Length - 1; i >= 0; i--)
            {
                Console.Write(getnumber[i]);
            }

Csharp program to print a Rhombus:

for (int i = 1; i <= 5; i++)
            {
                for (int j = i - 1; j > 1; j--)
                {
                    Console.Write(j);
                }
                for (int j = 1; j < i; j++)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }
            for (int i = 4; i > 0; i--)
            {
                for (int j = i - 1; j > 1; j--)
                {
                    Console.Write(j);
                }
                for (int j = 1; j < i; j++)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }

output : 

1
212
32123
4321234
32123
212
1

Csharp program to print Rhombus of numbers:

 int nu = 5;
            for (int i = 1; i <= nu; i++)
            {
                for (int j = 1; j < i; j++)
                {
                    Console.Write(j);
                }
                for (int j = i - 2; j > 0; j--)
                {
                    Console.Write(j);
                }
                Console.WriteLine();
            }

Output :
1
121
12321
1234321

Pattern-1:

output:

    1
   121
  12321
 1234321
123454321

Code:

            int nu = 5;
            for (int i = 1; i <= nu; i++)
            {
                int kk = ((nu-i) +(nu - i)) / 2;
                for (int y = 0; y < kk; y++)
                {
                    Console.Write(" ");
                }

                for (int j = 1; j <= i; j++)
                {
                    Console.Write(j);
                }
                for (int j = i - 1; j > 0; j--)
                {
                    Console.Write(j);
                }

                for (int y = 0; y < kk; y++)
                {
                    Console.Write(" ");
                }
                Console.WriteLine();
            }

Tuesday, September 11, 2018

Export HTML to Excel In window application c#.


 string str = @" <table>
                                    <thead>
                                      <tr>
                                        <th>Firstname</th>
                                        <th>Lastname</th>
                                        <th>Email</th>
                                      </tr>
                                    </thead>
                                    <tbody>
                                      <tr>
                                        <td>John</td>
                                        <td>Doe</td>
                                        <td>john@example.com</td>
                                      </tr>
                                      <tr>
                                        <td>Mary</td>
                                        <td>Moe</td>
                                        <td>mary@example.com</td>
                                      </tr>
                                      <tr>
                                        <td>July</td>
                                        <td>Dooley</td>
                                        <td>july@example.com</td>
                                      </tr>
                                    </tbody>
                                  </table>";
                File.WriteAllText(filename,str);

Friday, September 7, 2018

Value Type:

Value type contain it's own value .
for example:
int i=100;
Refrence Value Example:



Friday, August 31, 2018

Export Data from Dataset to Excell in c# :


   private void btnprint_Click(object sender, EventArgs e)
        {
            try
            {
                if (dataGridView1.Rows.Count <= 0)
                {
                    return;
                }

                DataTable dt2 = new DataTable();
                dt2 = (DataTable)dataGridView1.DataSource;
                DataSet ds1 = new DataSet();
                ds1.Tables.Add(dt2.Copy());

                string filename = "";
                SaveFileDialog saveFileDialog1 = new SaveFileDialog();
                saveFileDialog1.DefaultExt = "txt";
                saveFileDialog1.Filter = "Text files (*.xls)|*.xls|All files (*.*)|*.*";
                if (saveFileDialog1.ShowDialog() == DialogResult.OK)
                {
                    filename = saveFileDialog1.FileName;
                }

                using (var workbook = SpreadsheetDocument.Create(filename, DocumentFormat.OpenXml.SpreadsheetDocumentType.Workbook))
                {
                    var workbookPart = workbook.AddWorkbookPart();
                    workbook.WorkbookPart.Workbook = new DocumentFormat.OpenXml.Spreadsheet.Workbook();
                    workbook.WorkbookPart.Workbook.Sheets = new DocumentFormat.OpenXml.Spreadsheet.Sheets();

                    uint sheetId = 1;

                    foreach (System.Data.DataTable table in ds1.Tables)
                    {
                        var sheetPart = workbook.WorkbookPart.AddNewPart<WorksheetPart>();
                        var sheetData = new DocumentFormat.OpenXml.Spreadsheet.SheetData();
                        sheetPart.Worksheet = new DocumentFormat.OpenXml.Spreadsheet.Worksheet(sheetData);

                        DocumentFormat.OpenXml.Spreadsheet.Sheets sheets = workbook.WorkbookPart.Workbook.GetFirstChild<DocumentFormat.OpenXml.Spreadsheet.Sheets>();
                        string relationshipId = workbook.WorkbookPart.GetIdOfPart(sheetPart);

                        if (sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Count() > 0)
                        {
                            sheetId =
                                sheets.Elements<DocumentFormat.OpenXml.Spreadsheet.Sheet>().Select(s => s.SheetId.Value).Max() + 1;
                        }

                        DocumentFormat.OpenXml.Spreadsheet.Sheet sheet = new DocumentFormat.OpenXml.Spreadsheet.Sheet() { Id = relationshipId, SheetId = sheetId, Name = table.TableName };
                        sheets.Append(sheet);

                        DocumentFormat.OpenXml.Spreadsheet.Row headerRow = new DocumentFormat.OpenXml.Spreadsheet.Row();

                        List<String> columns = new List<string>();
                        foreach (DataColumn column in table.Columns)
                        {
                            columns.Add(column.ColumnName);

                            DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                            cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                            cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(column.ColumnName);
                            headerRow.AppendChild(cell);
                        }

                        sheetData.AppendChild(headerRow);

                        foreach (DataRow dsrow in table.Rows)
                        {
                            DocumentFormat.OpenXml.Spreadsheet.Row newRow = new DocumentFormat.OpenXml.Spreadsheet.Row();
                            foreach (String col in columns)
                            {
                                DocumentFormat.OpenXml.Spreadsheet.Cell cell = new DocumentFormat.OpenXml.Spreadsheet.Cell();
                                cell.DataType = DocumentFormat.OpenXml.Spreadsheet.CellValues.String;
                                cell.CellValue = new DocumentFormat.OpenXml.Spreadsheet.CellValue(dsrow[col].ToString()); //
                                newRow.AppendChild(cell);
                            }

                            sheetData.AppendChild(newRow);
                        }
                    }
                }

                System.Diagnostics.Process.Start(filename);
                ds1.Clear();
            }
            catch
            {
            }
        }

Wednesday, August 29, 2018

Generic & it's Example For Beginners :
A small Notes On generics.

By   using generics We can create a class with placeholder which are used for type of class members like method , variable .

For Example:

class Program
    {
        static void Main(string[] args)
        {
            mygenerics<int> mint = new mygenerics<int>();
            Console.WriteLine(mint.add(5, 6));

            mygenerics<string> mstr = new mygenerics<string>();
            Console.WriteLine(mstr.add("Rk", "Ki"));

            mygenerics<float> mfloat = new mygenerics<float>();
            Console.WriteLine(mfloat.add(20,3));

            mygenerics<double> mdouble = new mygenerics<double>();
            Console.WriteLine(mdouble.add(20.5, 3.05));

            Console.ReadKey();
        }
    }

    class mygenerics<t>
    {
        public t add(t a, t b)
        {
            t c = (dynamic)a + (dynamic)b;
            return c;
        }
    }


Check assigning items of a list to a new list of the same type but with an added property from Stack Overflow



https://stackoverflow.com/questions/52074938/assigning-items-of-a-list-to-a-new-list-of-the-same-type-but-with-an-added-prope