dim cuerpo as string="Donde va todo el texto del cuerpo"
Dim msg As New MailMessage(Me.txtEmail.Text, "info@dondevaya.com", "Prueba", cuerpo)
Dim mailClient As New SmtpClient("servidor SMTP", 25)
Dim cred As New System.Net.NetworkCredential("usuario", "clave")
msg.Attachments.Add(New Attachment(fupDoc.PostedFile.InputStream, fupDoc.FileName))
With mailClient
.UseDefaultCredentials = False
.Credentials = cred
.EnableSsl = False
.DeliveryMethod = SmtpDeliveryMethod.Network
.Send(msg)
End With
viernes, 24 de junio de 2011
miércoles, 16 de marzo de 2011
Consumir Servicio Web detras de un proxy
Dim svc As New Servicio
Dim nc As NetworkCredential = New NetworkCredential("usuario", "clave", "dominio")
Dim pr As New WebProxy("ip del proxy", puerto) 'puerto es un entero
pr.Credentials = nc
svc.Proxy = pr
Dim dtv As New DataView
Dim ds As New DataSet
ds = svc.getData
dtv = ds.Tables(0).DefaultView
Me.DataGridView1.DataSource = dtv
Al correrlo, ya podemos cargar el dataset devuelto en el datagrid.
Dim nc As NetworkCredential = New NetworkCredential("usuario", "clave", "dominio")
Dim pr As New WebProxy("ip del proxy", puerto) 'puerto es un entero
pr.Credentials = nc
svc.Proxy = pr
Dim dtv As New DataView
Dim ds As New DataSet
ds = svc.getData
dtv = ds.Tables(0).DefaultView
Me.DataGridView1.DataSource = dtv
Al correrlo, ya podemos cargar el dataset devuelto en el datagrid.
Etiquetas:
Consumir,
DataGridView,
Proxy,
Web Service
sábado, 12 de marzo de 2011
Consumir Servicio Web desde Silverlight
Debido a que he buscado y buscado por mucho tiempo en Google un método para guardar datos en Silverlight, me he dado la tarea de crear un servicio web para enviar los datos a una base de datos para poder hacer una inserción de registros.
Creacion del Servicio Web.
En el servicio web agregamos una referencia a MySql.Data.MySqlClient
Creamos el siguiente método web
[WebMethod]
public void insDocuments(int doc_type, string refr, byte[] imagen, int usr, DateTime dt, int st)
{
//System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
MySqlConnection cnn = new MySqlConnection("server=localhost;User Id=root;database=db_scanning");
MySqlCommand cmd = new MySqlCommand();
cnn.Open();
string sql = "insert into documents(id_document,doc_type,doc_reference,doc_scanned,user_id,mod_date,state)" +
" VALUES(@id_document,@doc_type,@doc_reference,@doc_scanned,@user_id,@mod_date,@state)";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Connection = cnn;
cmd.Parameters.Add("@id_document",MySqlDbType.VarChar).Value=getCode(doc_type);
cmd.Parameters.Add("@doc_type", MySqlDbType.Int32).Value = doc_type;
cmd.Parameters.Add("@doc_reference", MySqlDbType.VarChar).Value = refr;
cmd.Parameters.Add("@doc_scanned", MySqlDbType.LongBlob).Value = imagen;
cmd.Parameters.Add("@user_id", MySqlDbType.Int32).Value = usr;
cmd.Parameters.Add("@mod_date", MySqlDbType.Datetime).Value = dt;
cmd.Parameters.Add("@state", MySqlDbType.Int32).Value = st;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cnn.Close();
}
Despues lo invocamos desde la aplicacion Silverlight agregando la respectiva referencia a servicio.
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
BitmapImage imageSource = new BitmapImage();
imageSource.SetSource(op.File.OpenRead());
FileStream fst = op.File.OpenRead();
byte[] bytes = new byte[fst.Length];
fst.Read(bytes, 0, bytes.Length);
svc.insDocumentsAsync(2, "REF", bytes, 1, DateTime.Today, 1);
Con esto ya podemos trabajar insertando registros a una base de datos utilizando Silverlight.
Creacion del Servicio Web.
En el servicio web agregamos una referencia a MySql.Data.MySqlClient
Creamos el siguiente método web
[WebMethod]
public void insDocuments(int doc_type, string refr, byte[] imagen, int usr, DateTime dt, int st)
{
//System.Windows.Media.Imaging.BitmapImage bImg = new System.Windows.Media.Imaging.BitmapImage();
MySqlConnection cnn = new MySqlConnection("server=localhost;User Id=root;database=db_scanning");
MySqlCommand cmd = new MySqlCommand();
cnn.Open();
string sql = "insert into documents(id_document,doc_type,doc_reference,doc_scanned,user_id,mod_date,state)" +
" VALUES(@id_document,@doc_type,@doc_reference,@doc_scanned,@user_id,@mod_date,@state)";
cmd.CommandType = CommandType.Text;
cmd.CommandText = sql;
cmd.Connection = cnn;
cmd.Parameters.Add("@id_document",MySqlDbType.VarChar).Value=getCode(doc_type);
cmd.Parameters.Add("@doc_type", MySqlDbType.Int32).Value = doc_type;
cmd.Parameters.Add("@doc_reference", MySqlDbType.VarChar).Value = refr;
cmd.Parameters.Add("@doc_scanned", MySqlDbType.LongBlob).Value = imagen;
cmd.Parameters.Add("@user_id", MySqlDbType.Int32).Value = usr;
cmd.Parameters.Add("@mod_date", MySqlDbType.Datetime).Value = dt;
cmd.Parameters.Add("@state", MySqlDbType.Int32).Value = st;
cmd.ExecuteNonQuery();
cmd.Parameters.Clear();
cnn.Close();
}
Despues lo invocamos desde la aplicacion Silverlight agregando la respectiva referencia a servicio.
OpenFileDialog op = new OpenFileDialog();
op.ShowDialog();
BitmapImage imageSource = new BitmapImage();
imageSource.SetSource(op.File.OpenRead());
FileStream fst = op.File.OpenRead();
byte[] bytes = new byte[fst.Length];
fst.Read(bytes, 0, bytes.Length);
svc.insDocumentsAsync(2, "REF", bytes, 1, DateTime.Today, 1);
Con esto ya podemos trabajar insertando registros a una base de datos utilizando Silverlight.
Etiquetas:
C#,
Silverlight,
Visual Studio 2010,
Web Service
miércoles, 23 de febrero de 2011
Enlazar ListView con Dataset
Esta es la forma como enlazar un List View con un dataset creado a partir de codigo, no olvidar que la propiedad View del List View debe estar en Details.
private void Form1_Load(object sender, EventArgs e)
{
string strCnn = "CADENA DE CONEXION";
SqlConnection cnn = new SqlConnection(strCnn);
string sql = "select * from TABLA;
SqlCommand cmd = new SqlCommand(sql, cnn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
cnn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "TABLA");
cnn.Close();
//creamos los encabezados del ListView que provienen del dataset
foreach (DataColumn column in ds.Tables[0].Columns)
{
listView1.Columns.Add(column.ColumnName);
}
//obtenemos los valores provenientes de la tabla.
foreach (DataRow row in ds.Tables[0].Rows)
{
ListViewItem item = new ListViewItem(row[0].ToString());
for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
{
item.SubItems.Add(row[i].ToString());
}
listView1.Items.Add(item);
}
}
private void Form1_Load(object sender, EventArgs e)
{
string strCnn = "CADENA DE CONEXION";
SqlConnection cnn = new SqlConnection(strCnn);
string sql = "select * from TABLA;
SqlCommand cmd = new SqlCommand(sql, cnn);
SqlDataAdapter da = new SqlDataAdapter();
da.SelectCommand = cmd;
cnn.Open();
DataSet ds = new DataSet();
da.Fill(ds, "TABLA");
cnn.Close();
//creamos los encabezados del ListView que provienen del dataset
foreach (DataColumn column in ds.Tables[0].Columns)
{
listView1.Columns.Add(column.ColumnName);
}
//obtenemos los valores provenientes de la tabla.
foreach (DataRow row in ds.Tables[0].Rows)
{
ListViewItem item = new ListViewItem(row[0].ToString());
for (int i = 1; i < ds.Tables[0].Columns.Count; i++)
{
item.SubItems.Add(row[i].ToString());
}
listView1.Items.Add(item);
}
}
lunes, 14 de febrero de 2011
Linkbutton agregado en un gridview: para que hagamos lo que querramos con el:
LinkButton lnkSeleccion = (LinkButton)sender;
string arg = lnkSeleccion.CommandArgument;
Response.redirect("~/edicion.aspx?id="+ arg);
En la vista código debemos agregar el evento CommandArgument
Aqui id es el campo que vamos a enlazar de la tabla x, la cual ya debe estar enlazada al grid view
CommandArgument='<%# Eval("id) %> >Link
LinkButton lnkSeleccion = (LinkButton)sender;
string arg = lnkSeleccion.CommandArgument;
Response.redirect("~/edicion.aspx?id="+ arg);
En la vista código debemos agregar el evento CommandArgument
Aqui id es el campo que vamos a enlazar de la tabla x, la cual ya debe estar enlazada al grid view
CommandArgument='<%# Eval("id) %> >Link
martes, 1 de febrero de 2011
Creación dinámica de controles
Dim i, j As Integer
j = Me.txtCantidad.Text
For i = 0 To j - 1
Dim textBox As New TextBox
Panel1.Controls.Add(textBox)
Next
j = Me.txtCantidad.Text
For i = 0 To j - 1
Dim textBox As New TextBox
Panel1.Controls.Add(textBox)
Next
sábado, 29 de enero de 2011
Tipos Genericos en C#
Esta es la forma para implementar métodos genéricos en C#. Nos sirven para la reusabilidad y seguridad de tipos.
en estos ejemplos T reemplaza al tipo....
Ejemplo sencillo...
static void Main(string[] args)
{
int[] arrayEntero = { 1, 2, 3, 4, 5 };
double[] arrayDoble = { 1.2, 3.4, 5.6, 9.8, 10.2 };
MuestraArray(arrayEntero);
MuestraArray(arrayDoble);
Console.ReadLine();
}
private static void MuestraArray (T[] arrayEntrada)
{
foreach (T element in arrayEntrada)
Console.Write(element + " ");
Console.Write("\n");
}
}
Con restriccion...
static void Main(string[] args)
{
Console.WriteLine("maximo de {0}, {1}, {2} es {3}",3.2,4.9,3.1,Maximo(3.2,4.9,3.1));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", 3, 9, 30, Maximo(3,9,30));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", "a", "b", "c", Maximo("a","b","c"));
Console.ReadLine();
}
private static T Maximo(T x, T y, T z)
where T:IComparable
{
T max = x;
if (y.CompareTo(max) > 0)
max = y;
if (z.CompareTo(max) > 0)
max = z;
return max;
}
Aqui utilizamos el método genérico Máximo, de tipo T y variables tambien de tipo T, el tipo T nos quiere decir que puede ser cualquiera: int, double, string, etc.
en estos ejemplos T reemplaza al tipo....
Ejemplo sencillo...
static void Main(string[] args)
{
int[] arrayEntero = { 1, 2, 3, 4, 5 };
double[] arrayDoble = { 1.2, 3.4, 5.6, 9.8, 10.2 };
MuestraArray(arrayEntero);
MuestraArray(arrayDoble);
Console.ReadLine();
}
private static void MuestraArray
{
foreach (T element in arrayEntrada)
Console.Write(element + " ");
Console.Write("\n");
}
}
Con restriccion...
static void Main(string[] args)
{
Console.WriteLine("maximo de {0}, {1}, {2} es {3}",3.2,4.9,3.1,Maximo(3.2,4.9,3.1));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", 3, 9, 30, Maximo(3,9,30));
Console.WriteLine("maximo de {0}, {1}, {2} es {3}", "a", "b", "c", Maximo("a","b","c"));
Console.ReadLine();
}
private static T Maximo
where T:IComparable
{
T max = x;
if (y.CompareTo(max) > 0)
max = y;
if (z.CompareTo(max) > 0)
max = z;
return max;
}
Aqui utilizamos el método genérico Máximo, de tipo T y variables tambien de tipo T, el tipo T nos quiere decir que puede ser cualquiera: int, double, string, etc.
Suscribirse a:
Entradas (Atom)
