tag:blogger.com,1999:blog-81833871457049247692024-03-13T11:26:35.614-07:00Blog WebEn este espacio buscamos brindar el conocimiento obtenido a lo largo de 10 años, impulsar a los principiantes y seguir aprendiendoJavier Francohttp://www.blogger.com/profile/00597322010235434558noreply@blogger.comBlogger3125tag:blogger.com,1999:blog-8183387145704924769.post-27454670544785118582011-04-05T11:55:00.000-07:002011-04-05T13:35:49.179-07:00Como Crear Web Service ASP.NET<span style="font-family: inherit;">Se describe como <strong>Crear</strong> y Consumir un <strong>Web Service</strong> </span><br />
<div class="fullpost"><br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">En este apartado explicare como crear y consumir un <strong>Web Service</strong>.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">En este ejemplo creare 2 proyectos de manera separada, en el primero crearemos el web service y en el segundo la aplicación que lo consumirá.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span lang="EN-US" style="font-family: inherit; mso-ansi-language: EN-US;">-Creamos un <strong>ASP.NET</strong> Empty Web Application </span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Ahora creamos un nuevo ítem y seleccionamos <strong>Web Service</strong>, en mi caso lo nombre “WebServicesTest”, nuestro proyecto quedara de la siguiente manera:</span><br />
<span style="font-family: inherit;"><br />
</span><br />
<div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixaZM9NotLQFhiXnb60DvepkSW2eRCsH8NF1RnEVu4bpy7Iy1g3xcWM-fwqAo8I8sxlmc2Nsv6gKC4XMXlgk-lnqTfj39Qa9UB0X2uQ5eMKHip1wgOAXtm7V9Ep8V_Ylk0G3zj1gZDOhWO/s1600/Captura.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" r6="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEixaZM9NotLQFhiXnb60DvepkSW2eRCsH8NF1RnEVu4bpy7Iy1g3xcWM-fwqAo8I8sxlmc2Nsv6gKC4XMXlgk-lnqTfj39Qa9UB0X2uQ5eMKHip1wgOAXtm7V9Ep8V_Ylk0G3zj1gZDOhWO/s1600/Captura.PNG" /></span></a></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Ahora damos doble clic sobre el archivo Test.asmx.cs y modificamos<span style="mso-spacerun: yes;"> </span>el web method <span style="mso-spacerun: yes;"> </span>HelloWord()</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Quedara de la siguiente manera:</span><br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-family: inherit; font-size: 9.5pt; mso-ansi-language: EN-US;"> </span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-family: inherit; font-size: 9.5pt; mso-ansi-language: EN-US;"> <span style="color: yellow;"> [WebMethod]</span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>public string HelloWorld()</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>{</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>return "Hello World";</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 9.5pt;">}</span></span></span></div><span style="font-family: inherit;"><br />
</span><br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Ya tenemos listo nuestro web service, ahora lo que tenemos que hacer es publicarlo para que nuestro proyecto lo pueda consumir.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Para hacer la publicación hacemos lo siguiente:</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Crear una carpeta para indicar en donde se guardaran lo archivos publicados, en mi caso dicha carpeta se llama “WS” dentro de la unidad C.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Ahora en nuestro proyecto “WebServicesTest” damos clic derecho y seleccionamos la opción “publish” e indicamos la carpeta destino, la configuración quedara de la siguiente manera:</span></div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgz1N9carwdPGQCybY8XW-SAVvsRSDKvqeG60yY6hZrBuxQwMRxUlRPimruZFEjwyxfpoZaQtQ_5NFgaJngGuUyCPLYZoOXlvleOq0ZLkubosPuJam8Et3zC-xsZtITCuanA4EIwh1jcxPP/s1600/Captura2.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="302" r6="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEgz1N9carwdPGQCybY8XW-SAVvsRSDKvqeG60yY6hZrBuxQwMRxUlRPimruZFEjwyxfpoZaQtQ_5NFgaJngGuUyCPLYZoOXlvleOq0ZLkubosPuJam8Et3zC-xsZtITCuanA4EIwh1jcxPP/s320/Captura2.PNG" width="320" /></span></a></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: inherit;"><br />
</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Una vez terminada la publicación en la carpeta destino te habrá creado la carpeta bin, el Web Service y el Web Config, ahora crearemos nuestro directorio virtual en IIS haciendo lo siguiente.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Dentro de Defaul Web Site damos clic derecho/Agregar Aplicación</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Ponemos un Alias en mi caso puse “WS” Seleccionamos el pool en mi casa es “ASP:NET v4.0 Classic”, seleccionamos el origen de los archivos en mi caso “C:\WS” damos clic en Aceptar.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Ya tenemos nuestro web service publicado para validarlo abrimos nuestro explorador web, poniendo la siguiente dirección: </span><a href="http://localhost/WS/Test.asmx"><span style="color: white; font-family: inherit;">http://localhost/WS/Test.asmx</span></a></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Nos mostrara la siguiente pantalla:</span></div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL9kKf7N0QAoVAy9TjOeqZ2rFdpj7naWXiqw7pFldrJxJiWVnMX0VeJDxKAb3P64mlwvKjrD-4wHAPqHAIh1ikxYlLqeBEVKiZTxPBzhP_AntZmGASPV7ZNmLkyG1QeIkBC90y46jUeiSv/s1600/Captura3.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" height="176" r6="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiL9kKf7N0QAoVAy9TjOeqZ2rFdpj7naWXiqw7pFldrJxJiWVnMX0VeJDxKAb3P64mlwvKjrD-4wHAPqHAIh1ikxYlLqeBEVKiZTxPBzhP_AntZmGASPV7ZNmLkyG1QeIkBC90y46jUeiSv/s320/Captura3.PNG" width="320" /></span></a></div><div class="separator" style="clear: both; text-align: left;"><span style="font-family: inherit;"><br />
</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Si por alguna razón te marco error<span style="mso-spacerun: yes;"> </span>verifica los permisos de tu carpeta y la versión del framework</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Ahora vamos a consumir dicho web service.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Creamos un nuevo proyecto web en mi caso se llamara GetWS, agregamos un WebForm y hacemos la referencia a nuestro web service, el proyecto quedara de la siguiente manera:</span></div><div class="separator" style="clear: both; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDCzgESOHytVPAj5MtrL7v28hDlPPs7puHUpdtnC8aNjJiqX4P5cB8wQVWSiLnXtQFxJZR25TTxGXxAmiCWWcRrCcnHgy1eqxn5yWBodxI92KrqmUFa7uCRODAljaQcX4qYLl1wUFJFZUE/s1600/Captura4.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><span style="font-family: inherit;"><img border="0" r6="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEhDCzgESOHytVPAj5MtrL7v28hDlPPs7puHUpdtnC8aNjJiqX4P5cB8wQVWSiLnXtQFxJZR25TTxGXxAmiCWWcRrCcnHgy1eqxn5yWBodxI92KrqmUFa7uCRODAljaQcX4qYLl1wUFJFZUE/s1600/Captura4.PNG" /></span></a></div><span style="font-family: inherit;"><br />
</span><br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">Para agregar la referencia web damos clic derecho en el proyecto GetWS y seleccionamos la opción Add Web Reference, ahora en URL pegamos la dirección de nuestro web Service en mi caso es: </span><a href="http://localhost/WS/Test.asmx"><span style="color: white; font-family: inherit;">http://localhost/WS/Test.asmx</span></a><span style="font-family: inherit;"> y damos enter para que encuentre nuestros web methods, una vez que los encuentre ponemos el nombre de nuestra referencia en mi caso “WsHello” y damos<span style="mso-spacerun: yes;"> </span>clic en Add Reference.</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;">-Ahora en nuestro web form agregamos un label y un botón. El html quedara asi:</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><</span><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;">form</span><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"> id="form1" runat="server"></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 9.5pt;"><</span><span style="font-size: 9.5pt;">div</span><span style="font-size: 9.5pt;">></span><span style="font-size: 9.5pt;"></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span><asp:Label ID="lblHello" runat="server" Text="Hola Amigo"></asp:Label></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span><br /></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span><br /></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span><asp:Button ID="btnGetWs" runat="server" Text="Button" OnClick="btnGetWs_Click" /></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 9.5pt;"></</span><span style="font-size: 9.5pt;">div</span><span style="font-size: 9.5pt;">></span><span style="font-size: 9.5pt;"></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="color: yellow; font-family: inherit;"></form></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><br />
</span></div><span style="font-family: inherit;"></span><span style="font-size: 9.5pt;"><span style="color: blue;"></span></span><span style="font-family: inherit;"> </span> <br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="color: white; font-family: inherit; font-size: small;">-Ahora en el code behind de nuestro aspx agregamos el siguiente código en el evento del botón:</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;">protected</span><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"> void btnGetWs_Click(object sender, EventArgs e)</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>{</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>//Referencia al Web Service</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>WsHello.Test WSGet = new WsHello.Test();</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 9.5pt;">try</span><span style="font-size: 9.5pt;"></span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>{</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>//Asignamos a nuestro label el valor obtenido por el Web Method</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="font-size: 9.5pt;"><span style="mso-spacerun: yes;"> </span></span><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;">lblHello.Text = WSGet.HelloWorld();</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>}</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>catch (Exception ex)</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><span style="color: yellow;"><span lang="EN-US" style="font-size: 9.5pt; mso-ansi-language: EN-US;"><span style="mso-spacerun: yes;"> </span></span><span style="font-size: 9.5pt;">{</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>throw ex;</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>}</span></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-size: 9.5pt;"><span style="font-family: inherit;"><span style="color: yellow;"><span style="mso-spacerun: yes;"> </span>}</span></span></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: inherit;"><br />
</span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="color: black; font-family: inherit; font-size: small;">-Listo ya tenemos nuestro web service funcionando…</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: inherit;"><br />
</span></div></div></div></div>Javier Francohttp://www.blogger.com/profile/00597322010235434558noreply@blogger.com0tag:blogger.com,1999:blog-8183387145704924769.post-23224828326950572582011-02-21T08:59:00.000-08:002011-02-21T08:59:42.526-08:00Grid CSSSe describe como agregar estilo a un <strong>Gridview</strong> con <strong>CSS</strong>e imagenes.<br />
<div class="fullpost"><br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: Calibri;">Como dar formato a un <strong>gridview</strong> con <strong>css</strong></span></div><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: Calibri;">Creamos nuestro estilo</span></div><blockquote style="background: #666666; border-bottom: #999999 1px solid; border-left: #999999 1px solid; border-right: #999999 1px solid; border-top: #999999 1px solid; display: block; font-family: Trebuchet MS; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px;">.Grid<br />
{<br />
width: 100%;<br />
background-color: #fff;<br />
margin: 5px 0 10px 0;<br />
border: solid 1px #525252;<br />
border-collapse: collapse;<br />
}<br />
.Grid td<br />
{<br />
padding: 2px;<br />
border: solid 1px #c1c1c1;<br />
color: #717171;<br />
}<br />
.Grid th<br />
{<br />
padding: 4px 2px;<br />
color: #fff;<br />
background: #424242 url(Image/grd_head.png) repeat-x top;<br />
border-left: solid 1px #525252;<br />
font-size: 0.9em;<br />
}<br />
.Grid .alt<br />
{<br />
background: #fcfcfc url(Image/grd_alt.png) repeat-x top;<br />
}<br />
.Grid .pgr<br />
{<br />
background: #424242 url(Image/grd_pgr.png) repeat-x top;<br />
}<br />
.Grid .pgr table<br />
{<br />
margin: 5px 0;<br />
}<br />
.Grid .pgr td<br />
{<br />
border-width: 0;<br />
padding: 0 6px;<br />
border-left: solid 1px #666;<br />
font-weight: bold;<br />
color: #fff;<br />
line-height: 12px;<br />
}<br />
.Grid .pgr a<br />
{<br />
color: #666;<br />
text-decoration: none;<br />
}<br />
.Grid .pgr a:hover<br />
{<br />
color: #000;<br />
text-decoration: none;<br />
}<br />
<br />
<br />
</blockquote><span style="font-family: "Courier New", Courier, monospace;">Creamos nuestro grid view asignándole los estilos</span><br />
<br />
<span style="font-family: "Courier New", Courier, monospace;"><asp:GridView ID="grvUsuario" runat="server" Width="100%" AutoGenerateColumns="False" GridLines="None" AllowPaging="true" CssClass="Grid" PagerStyle-CssClass="pgr" AlternatingRowStyle-CssClass="alt"></span><br />
<span style="font-family: "Courier New", Courier, monospace;"> <Columns> </span><br />
<span style="font-family: "Courier New", Courier, monospace;"><asp:BoundField DataField="UNO" HeaderText="UNO" /></span><br />
<span style="font-family: "Courier New", Courier, monospace;"><asp:BoundField DataField="DOS" HeaderText="DOS" /></span><br />
<span style="font-family: "Courier New", Courier, monospace;"> <asp:BoundField DataField="TRES" HeaderText="TRES" /></span><br />
<span style="font-family: "Courier New", Courier, monospace;"><asp:BoundField DataField="CUATRO" HeaderText="CUATRO" /> </span><br />
<span style="font-family: "Courier New", Courier, monospace;"> </Columns></span><br />
<span style="font-family: "Courier New", Courier, monospace;"></asp:GridView></span><br />
<br />
<span style="font-family: Courier New;">Estas son las imagenes:</span><br />
<div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><br />
</div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none;"><br />
</div><div class="separator" style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; clear: both; text-align: left;"><img border="0" h5="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEieNhtUFyiIynezIiKvHwyXkjTUIJ6HZRVUvoQa-261FFTacG6yosZr8jy3SWC-enN4_-QeB6aVuWRWolXsExRfyOWdrecUAWA0lQlqdflgw-_p0byVkpO6biKUqpyvvS9sZeQzEFlmc7QL/s1600/grd_alt.png" /> <img border="0" h5="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiVoq5iGZ4O8dPDoE-lhD3Mw2Do5haFUqcthq9_MBWCfbkF0T4CKK6x8sWxKCQG7M8wvLDUmdKHNMCRuezd_8s4oPdQcv8Lz0Jhd7SwCWsgHYrzORQ7qtmPHRWEznWFXfbMv7toJLqFYucM/s1600/grd_head.png" /> <img border="0" h5="true" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEjlk259-5p5NE2Ta3ad0SbEd08esD-4srn9Gy1yMLeRx6K-BMyleia-63bit5D1nuZ88MgvYgAJHAcQUMH182HP7TdYz36fpFqoraW2cTBIw5AeLVDxJlQfdFcvRjs25wUwHhf69Qp5i4l2/s1600/grd_pgr.png" /></div><div style="border-bottom: medium none; border-left: medium none; border-right: medium none; border-top: medium none; text-align: left;"><br />
</div></div><br />
<br />
<br />
<br />
<br />
</div>Javier Francohttp://www.blogger.com/profile/00597322010235434558noreply@blogger.com0tag:blogger.com,1999:blog-8183387145704924769.post-24916196051663411512011-02-17T10:41:00.000-08:002011-03-28T14:52:37.479-07:00llenar gridview asp.netComo <strong>llenar</strong> un control <strong>gridview</strong> con c#, ADO.net, <strong>Stored Procedure </strong><br />
<div class="fullpost"><br />
<div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: "Courier New", Courier, monospace;">Iniciemos creando un Stored Procedure el cual regresa empleado, nombre, paterno, materno </span></div><blockquote style="background: #666666; border-bottom: #999999 1px solid; border-left: #999999 1px solid; border-right: #999999 1px solid; border-top: #999999 1px solid; display: block; font-family: Trebuchet MS; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px;">CREATE PROCEDURE [dbo].[GetEmployee]<br />
@id_emp int<br />
AS<br />
BEGIN<br />
IF @id_emp <> 0 <br />
select empleado,paterno,materno,nombre from empleados_tya<br />
where id_empleado=@id_emp<br />
ELSE<br />
select empleado,paterno,materno,nombre from empleados_tya<br />
END<br />
<br />
</blockquote><div class="MsoNormal" style="margin: 0cm 0cm 10pt;"><span style="font-family: "Courier New"; line-height: 115%; mso-no-proof: yes;">Una vez realizado este Stored procedemos con lo siguiente.</span><span style="font-family: "Courier New"; line-height: 115%; mso-no-proof: yes;"></span></div><blockquote style="background: #666666; border-bottom: #999999 1px solid; border-left: #999999 1px solid; border-right: #999999 1px solid; border-top: #999999 1px solid; display: block; font-family: Trebuchet MS; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px;">public DataSet CargaGrid(int iId)<br />
{<br />
string sCon = "Data Source=XAVIERF-PC;Initial Catalog=Eslabon_TyA_Lambi;User ID=****; Password=*****";<br />
SqlConnection oCOn = new SqlConnection(sCon);<br />
SqlDataAdapter da = new SqlDataAdapter("GetEmployee", sCon);<br />
da.SelectCommand.CommandTimeout = 0;<br />
da.SelectCommand.CommandType = CommandType.StoredProcedure;<br />
da.SelectCommand.Parameters.Add("@id_emp", SqlDbType.Int).Value = iId;<br />
DataSet ds = new DataSet();<br />
try<br />
{<br />
da.Fill(ds);<br />
da.Dispose();<br />
return ds;<br />
}<br />
catch (Exception ex)<br />
{<br />
throw ex;<br />
}<br />
<br />
}<br />
<br />
</blockquote><br />
<div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><span style="font-family: Consolas;"><span style="font-family: "Courier New", Courier, monospace;">Ahora Agregas el siguiente código HTML dentro del tag <body></span></span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
</div><div align="left" class="separator" style="clear: both; line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none; text-align: center;"></div><div class="separator" style="clear: both; line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none; text-align: left;"><a href="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE_89TpJVSaWF4YWwKRYFxXWBeTe6lv3r6j1zfYpgEbXUi7KW0faYqhBLnitM7-mUa09Pv5LJpe7nVUV6Z4rj6tRd6v0Hlq7L72xw_8LlLTemht6wlTpsbObM_1BevIdSG1sX-qWmWHq0A/s1600/code.PNG" imageanchor="1" style="margin-left: 1em; margin-right: 1em;"><img border="0" h5="true" height="142" src="https://blogger.googleusercontent.com/img/b/R29vZ2xl/AVvXsEiE_89TpJVSaWF4YWwKRYFxXWBeTe6lv3r6j1zfYpgEbXUi7KW0faYqhBLnitM7-mUa09Pv5LJpe7nVUV6Z4rj6tRd6v0Hlq7L72xw_8LlLTemht6wlTpsbObM_1BevIdSG1sX-qWmWHq0A/s320/code.PNG" width="320" /></a></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"><br />
<span style="font-family: "Courier New", Courier, monospace;">Este es el evento del botón, al dar clic caerá aquí</span></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"></div><div class="MsoNormal" style="line-height: normal; margin: 0cm 0cm 0pt; mso-layout-grid-align: none;"></div><br />
<blockquote style="background: #666666; border-bottom: #999999 1px solid; border-left: #999999 1px solid; border-right: #999999 1px solid; border-top: #999999 1px solid; display: block; font-family: Trebuchet MS; padding-bottom: 10px; padding-left: 10px; padding-right: 10px; padding-top: 10px;">protected void btnCarga_Click(object sender, EventArgs e)<br />
{<br />
try<br />
{<br />
//Creamos objeto de negocio<br />
BusCargaGrid oCargaGrid = new BusCargaGrid();<br />
int iIdEmp = Convert.ToInt32(txtEmp.Text==string.Empty ? "0" : txtEmp.Text);<br />
DataSet ds = oCargaGrid.CargaGrid(iIdEmp);<br />
grvEmpleados.DataSource = ds;<br />
grvEmpleados.DataBind();<br />
}<br />
catch (Exception ex)<br />
{<br />
string sScript = @"<script language="javascript" type="text/javascript">
";
sScript = sScript + @"alert('Ocurrio un error al carga grid "+ex.Message+"');";
sScript = sScript + @"
</script> ";<br />
Page.ClientScript.RegisterStartupScript(Page.GetType(), "Error Grid", sScript);<br />
}<br />
}<br />
<br />
</blockquote></div>Javier Francohttp://www.blogger.com/profile/00597322010235434558noreply@blogger.com6