Thursday, December 15, 2011

Hide/show Div using Jquery/Json

Earlier i explained Jquery validate dropdown list,Auto complete texbox in jquery,Date format, Cascading dropdownlist in asp.net. Here i will show a simple way to hide/ show Div or Jquery Toggle.For this i have drop down list with two items which are "General" and "Credit".Then add the on change event to Dropdown list which is call the jquery function when make a selection on drop down list

Default.aspx:
<asp:dropdownlist id="ddlptype" onchange="PopulatePatient();" runat="server">
<asp:listitem>General</asp:listitem>
<asp:listitem>Credit</asp:listitem>
</asp:dropdownlist>
 <div id="Organisation">
<table><tbody>
<tr><td><asp:label id="lblorgname" runat="server" text="Org name"></asp:label></td>     <td><asp:textbox id="txtorgname" runat="server"></asp:textbox></td></tr>
<tr><td><asp:label id="lblEmpCode" runat="server" text="EmpCode"></asp:label></td>     <td><asp:textbox id="txtempcode" runat="server"></asp:textbox></td>   </tr>
</tbody></table>
</div>
Script:
A small comparison is perform between Dropdown list selected value with patient type (General and Credit) to make this .if the drop down selected item value is equal to general , I show the organization details div and if no is selected, I hide the div.
function PopulatePatient() {
        var value = $('#<%=ddlptype.ClientID%>').val();
        if (value == "General") {
            var t = document.getElementById("Organisation");
            $("#Organisation").fadeOut("slow");
        }
        else
            $("#Organisation").fadeIn("slow");
    }

Wednesday, December 14, 2011

link button in gridview in asp.net

Earlier  we learned how to export Gridview data to word document,gridview to excel,paging and sorting in Gridview.bind data to dropdownlist in gridview in asp.net.Here i will shown how to perform the data modifications(edit,delete) using link button in grid view in asp.net.Basically we have auto generated options to do edit and delete in grid view .But here i am using link button control with custom functionality in grid view .
Grid view having The Template field(asp:template field) which is used to add the any server controls and Bound field is used to bind the particulate field (cityid,city name) from data base to grid view columns from database .The bound fields has property to add the desirable column to it i.e Data Field
default.aspx:
<div>
<asp:panel cssclass="panel_set " id="Panel2" runat="server">
</asp:panel>

<br />
<table align="center" tablecen="" valign="center ><tbody>
<tr>  <td><br />
<asp:label id="lblcityid" runat="server" text="CityID:"></asp:label></td> <td><br />
<asp:textbox id="Txtcityid" readonly="True" runat="server"></asp:textbox></td>  </tr>
<tr> <td><br />
<asp:label id="lblcityname" runat="server" text="CityName:"></asp:label></td> <td><br />
<asp:textbox id="txtcityname" runat="server"></asp:textbox></td> </tr>
<tr> <td><br />
<asp:button id="btnsave" onclick="btnsave_Click" runat="server" text="Save">      <asp:button id="btnupdate" onclick="btnupdate_Click" runat="server" text="Update">  </asp:button></asp:button></td> </tr>
</tbody> </table>
<asp:panel cssclass="pnel_grv" id="panelgridviews" runat="server">
<asp:gridview autogeneratecolumns="False" cssclass="grid_scroll " datakeynames="cityid" emptydatatext="There is No Records To Display" id="grvcity" onrowdeleting="DeleteRecord" runat="server">
<columns>

<asp:boundfield datafield="cityid" headertext="cityid">
<itemstyle horizontalalign="Center" width="20px"></itemstyle>
</asp:boundfield>

<asp:boundfield datafield="cityname" headertext="cityname">
<itemstyle horizontalalign="Center" width="20px"></itemstyle>
</asp:boundfield>

<asp:templatefield>
<headertemplate> </headertemplate>
<itemtemplate>
<asp:linkbutton id="Linkedit" onclick="Linkedit_click" runat="server">Edit</asp:linkbutton>
</itemtemplate>
</asp:templatefield>
<asp:templatefield headertext="Delete?">
<itemtemplate>

<asp:linkbutton causesvalidation="false" commandname="Delete" id="lnBD" runat="server" text="Delete"></asp:linkbutton>

</itemtemplate>
</asp:templatefield>
</columns>
        
<footerstyle backcolor="#99CCCC" forecolor="#003399">
<pagerstyle backcolor="#99CCCC" forecolor="#003399" horizontalalign="Left">
<selectedrowstyle backcolor="#009999" font-bold="True" forecolor="#CCFF99">
<headerstyle backcolor="#003399" font-bold="True" forecolor="#CCCCFF">       
</headerstyle></selectedrowstyle></pagerstyle></footerstyle></asp:gridview>
</asp:panel>
</div>

Code behind:
Here one pop up is coming to confirmation to delete the record when we click on the delete link button in grid view.one more thing will have to observe here is how to delete record event is raised.The OnRowDeleting command which has given to grid view properties(can see in aspx page) is used to delete the record in grid view
//edit 
protected void Linkedit_click(object sender, EventArgs e)
  {
   LinkButton lnkButton = sender as LinkButton;
   GridViewRow row = (GridViewRow)lnkButton.NamingContainer;
   lblgdeptid.Text = row.Cells[0].Text;
   cmd = new SqlCommand("editcity", con);
   cmd.CommandType = CommandType.StoredProcedure;
   cmd.Parameters.Add("cityid", SqlDbType.NVarChar).Value = lblgdeptid.Text;
   con.Open();
   SqlDataReader dr = cmd.ExecuteReader();
   if (dr.HasRows)
      {
       while (dr.Read())
             {
                Txtcityid.Text = dr["cityid"].ToString();
                txtcityname.Text = dr["cityname"].ToString();
              }
              dr.Close();
              con.Close();
        }
        else
        {
//give an pop with "there is no data"
        }
    }

//Delete

 protected void DeleteRecord(object sender, GridViewDeleteEventArgs e)
    {
     int cityid = Int32.Parse(grvcity.DataKeys[e.RowIndex].Value.ToString());
     CityBAL3 pBAL = new CityBAL3();
     pBAL.City_Delete(cityid);
     grvcity.EditIndex = -1;
 
    }

Monday, December 12, 2011

How to call a Stored Procedure in asp.net

Title: How to use stored procedure in asp.net using c#.net

Description:
In this article i will shown how to call stored procedure in asp.net application.If u want to call a procedure it may have 2 types of parameters
                                       1.input
                                       2.Output
Input parameters are used for taking a value into procedure for Execution.Output parameters are used for sending a value out of the procedure after execution.If a procedure has parameters to it for each of every parameter of this procedure a matching parameter has to be created by us under .net application>a parameter under the .net application has five different attributes to it.Those are
                                      1.Name
                                      2.Size
                                      3.SqlDb Type
                                      4.Value
                                      5.Direction
Where direction can be input or output>to create magic input parameter under the .net application you need to specify the attributes name and value while creation..To create output parameter under .net application you need to specify the attributes Name,sqlDbtype,Size,Direction.After execution of the procedure it will return to the value
Example:
If you want to call a procedure that returns a value from our .net application the method that has to be applied on the command is Execution non query.
//under page load
SqlConnection cn=new SqlConnection("Userid=sa;Password=123;Database=employee");
SqlCommnad cmd=New SqlCoammand();
cmd.commandtype=Comandtype.storedprocedure;
//Under button click event
try
{ 
cmd.parameter.clear();
cmd.parameter.AddwithVlaue(@empId",txtempid.Text);
cmd.parameter.Add(@empId",sqlDbtype.Varchar)direction=Parameterdirection.output;
cmd.parameter["empname"].Size=50;
cmd.parameter.Add(@add",sqlDbtype.Varchar)direction=Parameterdirection.output;
cn.Open();
cmd.ExcuteNonQuery();
Textbox2.Text=cmd.parameters["@empname"].value.Tostring();
Textbox3.Text=cmd.parameters["@empname"].value.Tostring();
}
catch(exception ex)
{
messageBox.show(ex.message);
}
finally{
cn.Close();
}
  

Friday, December 9, 2011

GridView Sorting and Paging in Asp.net

Title : Paging and sorting in grid view in asp.net using c#

Description:
Recently While working with the grid view sorting ,i have noticed the the functionality and do the example on in grid view using c sharp.Here i will shown how sort the data and pagination in grid view.The below specified code will make sorting when clicking on  grid view header .So first we have to do some properties set up i:e
Allow sorting-->True:-column headings will be provided with hyperlinks[link button]
Code behind:
//pageload
if(page.ispostback==false)
{
//ordinary request information will be displayed sorted based on the empname
Sqlconnection con=New Sqlconnection("userid=sa";password=;databse=emp");

SqlDataadaptor da=New SqlDataadapator("select*from Employee Orderby empname",con);

//orderby is used to retrieve records in sorted order
Dataset ds=New Dataset();

da.fill(ds,"Employee");

Gvemp.Datasource=ds.Table["Employee"];
Gvemp.DataBind();
}
Providing Logic For Grid view Event:-
When user clicks on the column header of grid view post back takes place,sorting event procedure of grid view will be executed.This Event procedure will be provided column name
protected void Gridview-sorting(Object sender,Event Args e)
{
Response.Write{"colname:"+e.SortExpression);
//e.SortExpression will provide colomn name selected by user
SqlDataadaptor da=New SqlDataadapator("select*from Employee Orderby empname"+e.Sortexpression,con);

Dataset ds=New Dataset();

da.fill(ds,"Employee");

Gvemp.Datasource=ds.Table["Employee"];

Gvemp.DataBind();
}
Note:The similar code is required in page load and Grid view-sorting event Process .To avoid repetition using fallowing subprogram to fill grid view
void fillgrid(cname);

Title: How to highlight the Grid view row in asp.net
Here i will shown how to high lighting the name according to country.This requires Row data bound event of grid view control.Row data bound event will be executed towards each row construction with data from data source.This will provide access to row constructed
protected void Gridview-rowdataBound(..,..)
{
Response.Write(e.row.cells[4].Text+"");

if(e.Row.cells[4].Text=="Usa")

e.Row.Backcolor=system.drawing.color.Red;
}

Wednesday, December 7, 2011

Difference between ADO and ADO.NET

Title:Difference between ADO and ADO.net

Below are some of the differences between ado.net and ado

ADO:
1.ADO are implemented using com technology
2.In the ADO we can store only one table in record set
3.Ado's doesn't support data relations
4.Ado's is not front end RDBMS
5.Ado's are connection oriented database management system
6.Using Ado's we can't integrate with XML
7.Using Ado's we can't generate sql statements
8.Ado,s are doesn't support data transactions

ADO.NET
1.ADO.NET are implemented using .net framework technology
2.In the ADO.NET we can store multiple tables in record set
3.Ado.NET support data relations
4.Ado.NET is not front end RDBMS
5.Ado.NET are connectionless oriented database management system
6.Using Ado.NET we can integrate with XML
7.Using Ado.NET we can generate sql statements
8.Ado.NET support data transactions

Bel