Tuesday, January 30, 2024

Arrays - One Dimensional - DSTC using C Language (Source Code Implemented)

Arrays - One Dimensional 
Implemented using C Language

 

Arrays have 2 basic operations i.e. extraction and storing values.

  • Extraction operation is a function that accepts an array (arr) and an index or subscript number ( i ) , and returns a values stored at index nuber. It is denoted as arr[i].

  • Storing operation accepts an array (arr), and index or subscript ( i ), and a value to be stored ( X ). It is denoted as arr[i] = X. Before a value has been assigned to an element of the array, its value is undefined and a reference to it in an expression is illegal.

C Language Code:

       ARRAY1D.H      

#include "assert.h"
typedef struct integer_array
{
int *bfr ;
int size;
}intarr ;

intarr *makearray(int n)
{
intarr *a1;
a1 = (intarr *) malloc(sizeof(intarr)) ;
a1->size = n ;
a1->bfr = (int *) malloc(sizeof(int)*n);
return (a1);
}

void input(intarr *a1,int val, int pos)
{
assert(pos>=0 && pos<a1->size);
assert(val != 0) ;
a1->bfr[pos] = val;
// or *(a1->bfr+pos) = v ;
}

int get(intarr *a1,int pos)
{
assert(pos>=0 && pos<a1->size);
return(a1->bfr[pos]);
// or return(*(a->bfr+pos));
}

void printarray(intarr *a1,int posupto)
{
// if posupto is zero(0) then it will all array elements otherwise
// only the speicified no. of elements.
int i=0,k ;
if (posupto==0)
i=a1->size ;
else
i = posupto ;

//DISPLAYS THE ARRAY BEFORE SORTING
printf("\n");
for(k=0;k<i;k++)
printf("a[%d]\t",k);

printf("\n");
for(k=0;k<i;k++)
printf("%d\t",a1->bfr[k]);
}

void sortarray(intarr *a1)
{
int i=0,j=0,t ;

for(i=0;i<a1->size-1;+i++)
{
for(j=i+1;j<a1->size;j++)
{
if ( a1->bfr[i] > a1->bfr[j] )
{
t = a1->bfr[i];
a1->bfr[i] = a1->bfr[j] ;
a1->bfr[j] = t ;
}
}
}

}

             ARRAY1D.CPP         

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <assert.h>
#include "array1d.h"

void main()
{
intarr *a1;
int i,val,n ;
printf("\n Enter the no.of elements in the array: ");
scanf("%d",&n);
printf("\nvalue entered wwas %d",n);
a1 = makearray(n);
printf("\nsize of array %d",a1->size);
for(i=0;i<n;i++)
{
printf("\n Enter values of a[%d]=",i);
scanf("%d",&val);
// a1->bfr[i] = val ;
input(a1,val,i);
}
printarray(a1,0) ;

sortarray(a1) ;
printarray(a1,0);
}


Rational Numbers (Abstract Data Type) - DSTC using C Language

Rational Numbers (Abstract Data Type) 
Implemented using C Language

 

Consider the ADT Rational, which is a Mathematical concept of a rational number. A rational number is a number that can be expressed as the quotient of two integers.

Operations to be performed for implemented Rational numbers are:

  • Creation of a rational number from two integers (rat *makerat(int a,int b))
  • Addition
  • Multiplication
  • Testing for Equality

C Language Code:

RATIONAL.H

typedef struct rational
{
int n;
int d;
}rat ;

rat *makerat(int a,int b)
{
rat *r ;
if (b==0)
{
printf("\n Den Not allowed as Zero ") ;
exit(1);
}
r = (rat *)malloc(sizeof(rat)) ;
r->n = a ;
r->d = b ;
return (r) ;
}

rat *sumrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) + (x->d * y->n) ;
r->d = x->d * y->d ;
return(r) ;
}

void killrat(rat *n)
{
free(n);
}

void printrat(rat *n)
{
printf("%d/%d",n->n,n->d);
}

rat *reducerat(rat *r)
{
rat *t ;
t = (rat *)malloc(sizeof(rat));
t = r ;
int i ;
if (t->n > t->d)
i = t->d;
else
i = t->n;

for(;i>1;i--)
{
if((t->n%i==0) && (t->d%i==0))
{
t->n = t->n/i ;
t->d = t->d/i ;
}
}

return(t);
}

 

rat *subtractrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) - (x->d * y->n);
r->d = x->d * y->d ;
return(r);
}

Get Paid by Reading Ads on your Mobiles

rat *multiplyrat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = x->n * y->n ;
r->d = x->d * y->d ;
return(r);
}

rat *dividerat(rat *x, rat *y)
{
rat *r ;
r = (rat *)malloc(sizeof(rat)) ;
r->n = (x->n * y->d) / (x->d * y->n);
r->d = 0 ;
return(r);
}

void ratequal(rat *x, rat *y)
{
if((x->n * y->d)== (x->d * y->n) )
printf("\n\t\tNumbers are Equal") ;
else
printf("\n\t\tNumbers are NOT Equal") ;

}

RATIONAL.CPP

#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <rational.h>

void main()
{
rat *m, *n, *p, *l, *q, *t ;
int i,j,k ;
clrscr();
printf("\n Enter 1st num and denomentor(n/d):");
scanf("%d/%d",&i,&j);
m = makerat(i,j);

printf("\n Enter 2st num and denomentor(n/d):");
scanf("%d/%d",&i,&j);
n = makerat(i,j);

// SUM OF Rational nos.
p = sumrat(m,n);

printf("\n Display Two Rational No.s sum ");
printrat(m) ;
printf(" + ");
printrat(n) ;
printf(" = ");
printrat(p) ;
t = reducerat(p) ;
printf(" = ");
printrat(t);

//********* SUBSTRATION OF 2 RATIONAL NOS.

q = subtractrat(m,n);

printf("\n Display Two Rational No.s Subtraction ");
printrat(m) ;
printf(" - ");
printrat(n) ;
printf(" = ");

printrat(q) ;
t = reducerat(q) ;
printf(" = ");
printrat(t);

killrat(m) ;
killrat(n) ;
killrat(p) ;
killrat(l) ;
killrat(q) ;
killrat(t) ;

}

Tuesday, January 24, 2023

How do I Insert data in SQL Server using ASP .Net

To insert data into a SQL Server database using ASP.NET, you can use the ADO.NET library to establish a connection to the database and execute an INSERT statement. Here is an example of how to do this:

1.    Create a new ASP.NET project and add a reference to the System.Data.SqlClient namespace.

2.    Create a new method that will handle the insertion of data.

3.    In this method, create a new SqlConnection object and set the connection string to the appropriate value for your SQL Server database.

4.    Open the connection using the Open() method.

5.    Create a new SqlCommand object and set the CommandText property to the INSERT statement you want to execute.

6.    Add any necessary parameters to the SqlCommand object using the Parameters.Add() method.

7.    Execute the command using the ExecuteNonQuery() method.

8.    Close the connection using the Close() method.

Here is an example of how your code might look:

using (SqlConnection con = new SqlConnection("Data Source=myServer;Initial Catalog=myDB;User ID=myUsername;Password=myPassword"))

{

    con.Open();

    using (SqlCommand cmd = new SqlCommand("INSERT INTO myTable (col1, col2) VALUES (@val1, @val2)", con))

    {

        cmd.Parameters.AddWithValue("@val1", "some value");

        cmd.Parameters.AddWithValue("@val2", 123);

        cmd.ExecuteNonQuery();

    }

    con.Close();

 

 

How do I retrieve data from SQL Server using ASP .Net

In order to retrieve data from a SQL Server database in an ASP.NET application, you can use ADO.NET, which is a set of classes that provide data access services for the .NET Framework. The most commonly used classes for this purpose are the SqlConnection, SqlCommand, and SqlDataReader classes.

Here is an example of how you can use these classes to retrieve data from a SQL Server database and display it in an ASP.NET page:

Create a new SqlConnection object and set its connection string to the appropriate value for your SQL Server database.

Create a new SqlCommand object and set its CommandText property to the SQL query you want to execute.

Open the SqlConnection.

Execute the query by calling the ExecuteReader method of the SqlCommand object and store the result in a SqlDataReader object.

Iterate through the SqlDataReader object and retrieve the data you need.

Close the SqlConnection and SqlDataReader.

Here is an example of the above steps

Sample Code:


using (SqlConnection con = new SqlConnection(connectionString))
{
    using (SqlCommand cmd = new SqlCommand("SELECT * FROM Customers", con))
    {
        con.Open();
        using (SqlDataReader reader = cmd.ExecuteReader())
        {
            while (reader.Read())
            {
                // Retrieve data from the reader
                string name = reader["Name"].ToString();
                string city = reader["City"].ToString();
                // Do something with the data
            }
        }
        con.Close();
    }
}


It's also important to consider that this is just a simple example, and in production applications, you should handle exceptions, use parameters to avoid SQL injection, and dispose objects properly.

Thursday, December 3, 2020

How to Shrink SQL Server Database Log File Size using DBCC Command

 

 

How to Shrink SQL Server Database Log File Size using DBCC Command

you must take a full backup of your database before executing the following: 

 

ALTER DATABASE <YOUR DATABASE NAME>
SET RECOVERY SIMPLE
GO
DBCC SHRINKFILE (<YOUR DATABASE LOG FILE NAME>, TARGET SIZE IN MB)
GO
ALTER DATABASE <YOUR DATABASE NAME>
SET RECOVERY FULL 

 

 

Wednesday, April 24, 2019

Reset Windows 2008 R2 Server Admin password


To Reset Administrator Password in Windows 2008 R2 Server is a reboot and a few steps:
  • Boot from the Micrsoft Windows Server 2008 DVD
  • From the Install Windows menu, click “Next”.
  • Select “Repair your computer”
  • In the System Recovery Options, select the Operating System instance that you wish to repair and click “Next”.
  • Select “Command Prompt”. The
  • At the command prompt, run the following commands:c:
    cd windows\system32
    ren Utilman.exe Utilman.exe.old

    copy cmd.exe Utilman.exe
  • Reboot the server allowing Windows to load as normal
  • At the logon screen, press Windows Key + U.
  • As the command prompt, enter the following command: 
    • net user administrator New_PASSWORD
  • Log into the server with New_PASSWORD
  • Reboot into the repair command prompt
c:
cd windows\system32
del utilman.exe
copy Utilman.exe.old utilman.exe
Reboot and enjoy

Thursday, May 10, 2018

ASP .NET AJAX - Maintain Or Set Page Scroll Position After Asynchronous Postback In ASP.NET AJAX



Now just add the below JavaScript code to the relevant section of your Web Page.


  1. <script type="text/javascript">  
  2.     var xPos, yPos, needScroll;  
  3.     var prm = Sys.WebForms.PageRequestManager.getInstance();  
  4.     prm.add_beginRequest(BeginRequestHandler);  
  5.     prm.add_pageLoaded(EndRequestHandler)  
  6.   
  7.     function BeginRequestHandler(sender, args) {  
  8.         xPos = 0;  
  9.         yPos = window.pageYOffset || document.documentElement.scrollTop;  
  10.     }  
  11.   
  12.     function EndRequestHandler(sender, args) {  
  13.         if (needScroll) {  
  14.             window.setTimeout("window.scrollTo(" + xPos + "," + yPos + ")", 100);  
  15.         }  
  16.     }  
  17. </script>  


After adding the above code at page script Tag on your code behind you need to add the below code at your .cs page to call the Javascript function after page completes its rendering after postback.


ScriptManager.RegisterStartupScript(this.Page, this.Page.GetType(), "ScrollTo", "var needScroll = true;", true);