Visualizzazione post con etichetta c#. Mostra tutti i post
Visualizzazione post con etichetta c#. Mostra tutti i post

martedì 3 giugno 2008

Confrontare IP con C#

In questo momento non ho tempo per approfondire l'argomento cmq se avete bisogno di confrontare due indirizzi ip o per controllare se un indirizzo ip appartiene ad un range di indirizzi ip ho scritto un pò di codice:

using System;
using System.Text;

namespace IPManager
{
class Program
{
static void Main(string[] args)
{
string cmd;
Console.WriteLine("^^^^^^^^^^^^PROG DI TEST PER IP^^^^^^^^^^^^");
Console.WriteLine("-> INSERISCI UN IP NEL FORMATO 192.168.1.1: ");
cmd = Console.ReadLine();
IPAddress ip = new IPAddress(cmd);
Console.WriteLine("-> HAI INSERITO: " + ip.ToString() + "");
Console.WriteLine("-> LO CONVERTO IN LONG: " + ip.Parse(ip.ToString()).ToString() + "");
Console.WriteLine("----------------------------------------");
Console.WriteLine("-> INSERISCI UN RANGE DI IP PER: " + ip.ToString() + "");
Console.WriteLine("-> DA: ");
cmd = Console.ReadLine();
IPAddress ipA = new IPAddress(cmd);
Console.WriteLine("-> A: ");
cmd = Console.ReadLine();
IPAddress ipB = new IPAddress(cmd);
if (ip.isInRange(ipA, ipB))
{
Console.WriteLine("-> " + ip.ToString() + " E' NEL RANGE!");
}
else
{
Console.WriteLine("-> " + ip.ToString() + ">NON< E' NEL RANGE!");
}
}
}
class IPAddress
{
private string ipStr;
public long NumericIPAddress
{
get { return ipNum; }
set { ipNum = value; }
}
private long ipNum;

public IPAddress(string ipAddress)
{
this.ipStr = ipAddress;
this.ipNum = this.Parse(ipAddress);
}

public long Parse(string ipAddress)
{
string[] arr;
arr = ipAddress.Split('.');
return (long.Parse(arr[0]) * 16777216) + (long.Parse(arr[1]) * 65536) + (long.Parse(arr[2]) * 256) + long.Parse(arr[3]);
}
public bool isInRange(IPAddress ipA, IPAddress ipB)
{
if ((ipA.ipNum <= this.ipNum) && (ipB.ipNum >= this.ipNum))
{
return true;
}
else
{
return false;
}
}
public override string ToString()
{
return ipStr;
}
}
}

sabato 3 maggio 2008

Note sul DataBind del DropDownList (ASP.NET)

Quando si effettua il DataBind di un controllo DropDownList con una qualsiasi sorgente dati nel Page_Load, fare attenzione che questo avvenga solo al primo Load e non ad ogni PostBack altrimenti un eventuale metodo sul OnSelectedIndexChanged non darebbe i risultati sperati.

tenere sempre a mente:
  • OnSelectedIndexChanged;
  • IsPostBack;
  • DataTextField;
  • DataValueField;
  • SelectedItem.Value.
beh visto così non dice nulla ma ci se ne futte no? ^^

sabato 22 marzo 2008

.NET SQL c# recuperare l'id del record appena inserito

E' di vitale importanza recuperare l'effettivo id (o di una qualsiasi chiave) di un record appena inserito con una INSERT. Di seguito il codice in c# :

string query = "INSERT INTO Blog (titolo, contenuto, data, dataCreazione, bozza) ";
query += "VALUES (@Titolo, @Contenuto, @Data, @DataCreazione, @Bozza); ";
query += "SET @PostID = SCOPE_IDENTITY()";
SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["DBTEST"].ConnectionString);
SqlCommand cmd = new SqlCommand(query, conn);
cmd.CommandType = CommandType.Text;

SqlParameter TitoloParameter = new SqlParameter("@Titolo", SqlDbType.VarChar, 255);
TitoloParameter.Value = tbTitolo.Text;
cmd.Parameters.Add(TitoloParameter);

SqlParameter ContenutoParameter = new SqlParameter("@Contenuto", SqlDbType.NText);
ContenutoParameter.Value = tbContenuto.Text;
cmd.Parameters.Add(ContenutoParameter);

SqlParameter DataParameter = new SqlParameter("@Data", SqlDbType.DateTime, 8);
DataParameter.Value = dataPostStr;
cmd.Parameters.Add(DataParameter);

SqlParameter DataCreazioneParameter = new SqlParameter("@DataCreazione", SqlDbType.DateTime, 8);
DataCreazioneParameter.Value = DateTime.Now.ToShortDateString();
cmd.Parameters.Add(DataCreazioneParameter);

SqlParameter BozzaParameter = new SqlParameter("@Bozza", SqlDbType.Bit, 1);
BozzaParameter.Value = true;
cmd.Parameters.Add(BozzaParameter);

SqlParameter IDParameter = new SqlParameter("@PostID",SqlDbType.Int);
IDParameter.Direction = ParameterDirection.Output;
cmd.Parameters.Add(IDParameter);

conn.Open();
cmd.ExecuteNonQuery();
int idPost = (int)IDParameter.Value;
conn.Close();

this.lblIdPost.Text = "ID del post inserito: " + idPost.ToString();

In rosso il codice più importante...
se avete problemi a leggere il codice provate con questa vaersione: Insert scope_identity in c#

Happy SQL

lunedì 10 settembre 2007

Impostare un TagPrefix predefinito ai WebCustomControls

Per impostare il TagPrefix in un progetto Libreria di controlli web in modo da non avere quello standard <cc1: basta andare nell' AssemblyInfo.cs (vedi fugura):


Imports o using, a seconda del linguaggio utilizzato di: System.Web.UI;
per impostare il TagPrefix aggiungere:

[assembly: TagPrefix ("TestControl"), ("lol")]

dove TestControl è il namespace del controllo e lol il prefisso.

Happy Coding!

mercoledì 2 maggio 2007

ASP.net Crea e Cancella i Cookies con C#

using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

public partial class prova : System.Web.UI.Page
{
public string str = DateTime.Now.ToString();
public string nome = "NOME_BISCOTTO";
public string scadenza;
public HttpCookie biscotto;

protected void Page_Load(object sender, EventArgs e)
{

biscotto = Request.Cookies[nome];
if (biscotto is object) { str = biscotto.Value.ToString(); }
}
protected void Button1_Click(object sender, EventArgs e)
{
biscotto = new HttpCookie(nome, "Contenuto del biscotto");
//biscotto.Expires = new DateTime(2100, 10, 10); va bene
biscotto.Expires = DateTime.MaxValue;
Response.AppendCookie(biscotto);
scadenza = biscotto.Expires.ToString();
}
protected void Button2_Click(object sender, EventArgs e)
{
biscotto = Request.Cookies[nome];
if (biscotto is object)
{
biscotto.Expires = new DateTime(1980, 10, 10);
//biscotto.Expires = DateTime.MinValue; non funziona
Response.AppendCookie(biscotto);
scadenza = biscotto.Expires.ToString();
}else scadenza = "IL COOKIE NON ESISTE";
}
}

venerdì 23 marzo 2007

C# passaggio di parametri

Differenze tra in out e ref:

  • in di default (di solito si omette), al termine delle oprazioni il valore viene perso;
  • out la variabile passata come argomento viene settata;
  • ref la variabile passata come argomento viene settata ma deve essere necessariamente inizializzata prima della chiamata;
getDati(out int colonna, out string valore)