Sunday, 20 October 2013

Sql command set | DML(Data Manipulation language) | Insert | Update | Delete

Sql command set | DML(Data Manipulation language) | Insert | Update | Delete
What is DML?
"Sql command set".
1-> "DML(Data Manipulation language)".
DML includes select ,insert , update , delete .
Let's study all there functionality one by one.

Database names:Stud
Table Name:student

i> Select;
we use select to fetch data from table or we can say to get record from table
use stud
select * from student.
here we can apply condition also like using where clause.
select * from student  where id=234

ii>Insert :
to insert  new record  in respected table we use insert.
insert into student values ("enter records you want to insert").


iii>Update 
to done modification in existing record.
update table table_name set column_name name where clause.

iv>Delete:
to delete unwanted records from table.
delete from table_name where column_name =parameter;

sql command set | DDL(Data definition language). | create | Alter | Drop

sql command set | DDL(Data definition language). | create | Alter | Drop
"Sql command set".
1-> "DDL(Data definition language)".
DDL includes Create ,alter , drop,truncate , comment , rename.
Let's study all there functionality one by one.

i->Create
 Create used  to create new table and object.
or you can create many tables and objects.
examples.
to Create table.
Create table student
(
id int identity primary key,
name varchar(20),
roll_no int
)
ii->Alter
 Used  to modify structure of table and other object..
alter table student alter name varchar(100)
using this query we can change size of column .
alter table student add  fee int.

iii->drop
Drop is used to drop table or column of table or object.
alter table student drop column name.
drop table student.
drop database stud.
we can drop database also by using drop keyword or command.


iv->Truncate 
Truncate use to delete all data present in the table.
But not table structure means , table will be there but it won't show any of record.

Truncate table Table_name
Truncate table student.

v->Comments
to add comments in procedure.

sp_insertrecord --'1','abcval'


vi->Rename
to rename  single table or multiple table in same database.
to rename single or multiple column in same table.

sp_rename 'existtablename' ,'renamedtablename'
SP_RENAME 'student.roll_no' ,'roll'  --to rename column name

SP_RENAME 'student' ,'stud'

sql server 2005/2008 | primary key | Identity key | Auto increment

sql server 2005/2008 | primary key | Identity key | Auto increment
how to set identity by query in table .
while creating by query in sql server 2008-2005 if you want to add
primary key with identity then you need to add primary key with identity at same time.
or if  you have already added then alter that  column but need to take care of  it.
here i have shown how to add identity with primary key in sql server 2005 and sql server 2008.
db name is stud
and table name is student.

user stud
create table student
(
id int NOT NULL primary key identity ,
roll int not null,
name varchar(30) not null

)

Wednesday, 9 October 2013

how to Fetch data from .aspx page to .cs or .vb page using javascript | Attribute.Add | Gridview_RowCommand |

In this article  I have shown how to retrieve or fetch data or value from .aspx page to .cs or .vb page .
I have used here c#. now there is two method to   fetch .aspx value from .aspx page to .cs page.
How to use Confirm box in asp.net.
1->Ajax
2->Javascript.
3-Javascript with Attribute.Add Property.
Now suppose i want to display confirm box and after that if user is saying
or clicking yes then execute function else  exit.
In here .cs page i have tried same functionality with GridView_rowcommand event.
suppose user want to delete clicked row from table or we can say grid.
what he will do, firstly he will click on delete link button  , actually i have taken linkbutton into the grid.
after clicking delete link button Confirm Box will appear (poped up)
then according to yes or cancel selection function will be execute.
consider user  has clicked delete link button then ..
((LinkButton)(row.FindControl("lnkbtndel"))).Attributes.Add("onclick", "javascript:return GetConfirmval();");

GetConfirmval() function will be called which is javascript function .
now comes to javascript..


 <script type ="text/javascript" >
    function GetConfirmval()
    {
    var value;
var val=confirm ("Are you sure want to delete selected record ");
   if (val==true )
   {
   document .getElementById ('<%=hdnEditdel.ClientID%>').value=1;
   }
    else
    {
    document .getElementById ('<%=hdnEditdel.ClientID%>').value=2;
    }
   
    }
    </script>

what i did here is , taken one hiddenfield  and assigned  different values according to requirement.
what happens when user click on yes on confirm box hiddenfield values will be set 1.
else 2.

now this  value we will use  .cs page .


protected void GridView1_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       LinkButton lnkbtn = (LinkButton)e.CommandSource ;
           GridViewRow row = (GridViewRow)lnkbtn.NamingContainer;

if (hdnEditdel.Value==Convert.ToString ("1")) // if value 1 means user clicked yes on confirm box.
           {

               Response.Write("1");

               DataTable dt = (DataTable)(ViewState["NewFirstTable"]);//stored data
               dt.Rows.RemoveAt(row.RowIndex);
               GridView1.DataSource = dt;
               GridView1.DataBind();
           }
           else
           {
               Response.Write("2");

           }
           hdnEditdel.Value = "";
}


now here is one way but in this method user need to click twice on link button which i have already used on gridview.
so to avoid that issue what i did i just took  OnlientClick  event and  call javascript function over there.
because that is perfect.
<asp:LinkButton runat="server" ID="lnkbtndel" Text ="Delete" CommandName ="Del" OnClientClick ="return GetConfirmval()"></asp:LinkButton>
step->
1->To use attribute remove onclient click on link button .
2->To use onclient click remove attribute from  rowcommand.


Tuesday, 8 October 2013

Temporary Table In Asp.Net | C# | Gridview Bind | Edit Delete In single Click

How to create temp table in  code behind and bind with Gridview control in asp.net.
In this article i have shown and given code to edit or display only those value from grid to respective text box on which row user will click.if he had clicked on 3rd row then all the data from third row will come up and  will display in respective control.
Steps-
1.Take viewstate to store first value as user will enter .
2.Take actual table (temporary table not linked with database no need any connection for this).
3.Take number of column as per requirement .
4.Take datarow  to add column in to datatable.
------------------------------------------------------------------------------------------------
Now here is code to create temporary table.
   private DataTable  ProConnection()
    {
        //ClsBusinessConnection objBus = new ClsBusinessConnection();
        //DataTable BindAllVal = objBus.DClsBusinessConnection();
        //gridtodisplaytempData.DataSource = BindAllVal;
        // gridtodisplaytempData.DataBind();

//above  4 line code need to  bind gridview with database  connectivity.
//but  currently if you don't have database and you need to do some gridview functionality //then you can create temporary table.
        DataTable temporarytableDt = new DataTable();
        if (ViewState["NewFirstTable"] == null)
        {  temporarytableDt.Columns.Add("Id");
            temporarytableDt.Columns.Add("Name", typeof(String));}
        else{
            temporarytableDt = (DataTable)(ViewState["NewFirstTable"]);}
        DataRow datarowvar = temporarytableDt.NewRow();
        datarowvar["Id"] = TxtFirstVal .Text;
        datarowvar["Name"] =TxtSecondVal .Text;
        temporarytableDt.Rows.Add(datarowvar);
        ViewState["NewFirstTable"] = temporarytableDt;
        gridtodisplaytempData.DataSource = temporarytableDt;
       gridtodisplaytempData.DataBind();
         return temporarytableDt;
    }
------------------------------------------------------------------------------------------
Now what i am goin to do is to take commandsource and NamingContainer so ,
i can get rowindex which has already edit and delete linkbutton,
to edit and delete the data from temporary table and bind to the grid again.
No Need to take two function for edit and delete just take rowcommand and use command name .
but for adding purpose we need to take add function because that is not inside the grid.

  public void gridtodisplaytempData_Click(object sender , System .EventArgs e)
   {
       ProConnection();
   }

->Dont forget to add edit event..
 protected void gridtodisplaytempData_RowEditing(object sender, GridViewEditEventArgs e)
   {}
  protected void gridtodisplaytempData_RowCommand(object sender, GridViewCommandEventArgs e)
   {
       LinkButton lnkbtn = (LinkButton)e.CommandSource ;
           GridViewRow row = (GridViewRow)lnkbtn.NamingContainer;
       if (e.CommandName == "Edit")
       {
           TxtFirstVal.Text = ((Label)(row.FindControl("lblId"))).Text;
           TxtSecondVal.Text = ((Label)(row.FindControl("lbltitle"))).Text;
       }
         
       else
       {

           DataTable dtnew = (DataTable)(ViewState["NewFirstTable"]);
           dtnew.Rows.RemoveAt(row.RowIndex);
gridtodisplaytempData.DataSource = dtnew;
gridtodisplaytempData.DataBind();
                                  }
   }

now when you click edit link button ..
if (e.CommandName == "Edit")
       {
         
           TxtFirstVal.Text = ((Label)(row.FindControl("lblId"))).Text;
           TxtSecondVal.Text = ((Label)(row.FindControl("lbltitle"))).Text;
       }

this code will get execute and for delete ..
  DataTable dtnew = (DataTable)(ViewState["NewFirstTable"]);
           dtnew.Rows.RemoveAt(row.RowIndex);
gridtodisplaytempData.DataSource = dtnew;
gridtodisplaytempData.DataBind();

it will take directly rowindex and will romove that row from temporary  table.
--------------------------------------------------------------------------------

    <div>
    <table >
 
    <tr><td>Enter Id:</td><td>:</td><td><asp:TextBox  runat="server" ID="TxtFirstVal"></asp:TextBox> </td></tr>
    <tr><td>Enter Name:</td><td>:</td><td><asp:TextBox  runat="server" ID="TxtSecondVal"></asp:TextBox> </td></tr>
    <tr>
    <td colspan ="3">
 
    <asp:Button runat="server" ID="btnSubmit" Text ="Submit"
            onclick="btnSubmit_Click" />
         
            <asp:Button runat="server" ID="btnAdd" Text ="Add" onclick="btnAdd_Click"/>
            <asp:label runat ="server" ID="lblnum" ></asp:label>
     
        <asp:GridView ID="gridtodisplaytempData" runat="server" AutoGenerateColumns ="false"
            GridLines="Both"  AllowSorting ="true" PageSize ="5"
         

            onrowcreated="gridtodisplaytempData_RowCreated"
 onrowediting="gridtodisplaytempData_RowEditing"  >
        <HeaderStyle/>
        <Columns >
        <asp:TemplateField  ><ItemTemplate >
        <asp:LinkButton runat="server" ID="lnkbtndel" Text ="Delete" CommandName ="Del"></asp:LinkButton>
        <asp:LinkButton runat="server" ID="lnkbtn"  Text ="Edit" CommandName ="Edit"></asp:LinkButton>
        <asp:label runat="server" id="lblId" SortExpression ="dt"  Text='<%#Eval("Id") %>'  HeaderText ="ID"

HeaderStyle-CssClass ="CssClass"></asp:label> </ItemTemplate></asp:TemplateField>
        <asp:TemplateField HeaderText ="Title" SortExpression ="UsrScrM_Title"><ItemTemplate >
        <asp:label runat="server" id="lbltitle"  Text='<%#Eval("Name") %>'  HeaderText ="ID"  HeaderStyle-CssClass

="CssClass"></asp:label> </ItemTemplate></asp:TemplateField>
     
   
        </Columns>
     
        </asp:GridView>
    </td>
    </tr>
    <tr><td>&nbsp;</td> </tr>
 
    </div>