|
ProgrammaSubito.it
|
|
|
|
|
|
|
|
|
|
|
|
Accesso ai Dati
File Default.aspx
<%@ Page Language="C#" AutoEventWireup="true"
CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head id="Head1" runat="server">
<title>Untitled Page</title>
<script language="javascript" type="text/javascript">
function conferma()
{
if(!confirm('Confirm?')) return false;
return true;
}
</script>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1"
runat="server"
AutoGenerateColumns="False"
DataKeyNames="ID"
OnSelectedIndexChanged="GridView1_SelectedIndexChanged"
CellPadding="4"
OnRowDeleting="GridView1_RowDeleting" >
<Columns>
<asp:BoundField DataField="ID" HeaderText="ID"
InsertVisible="False" ReadOnly="True"
SortExpression="ID" />
<asp:BoundField DataField="Nome" HeaderText="Nome"
SortExpression="Nome" />
<asp:BoundField DataField="Cognome" HeaderText="Cognome"
SortExpression="Cognome" />
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton1" runat="server"
CausesValidation="False" CommandName="Select"
Text="Select"></asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
<asp:TemplateField ShowHeader="False">
<ItemTemplate>
<asp:LinkButton ID="LinkButton2" runat="server"
CausesValidation="False" CommandName="Delete"
Text="Delete" OnClientClick="return conferma()">
</asp:LinkButton>
</ItemTemplate>
</asp:TemplateField>
</Columns>
</asp:GridView>
<br />
<br />
<asp:TextBox ID="TextBox_Nome" MaxLength="30" runat="server">
</asp:TextBox>
<asp:TextBox ID="TextBox_Cognome" MaxLength="30" runat="server">
</asp:TextBox>
<asp:Button ID="Button1" runat="server"
Text="Modifica"
OnClick="Button1_Click" />
<br />
<br />
<asp:TextBox ID="TextBoxNome_Ins" MaxLength="30" runat="server">
</asp:TextBox>
<asp:TextBox ID="TextBoxCognome_Ins" MaxLength="30" runat="server">
</asp:TextBox>
<asp:Button ID="Button_Inserisci" runat="server"
Text="Inserisci" OnClick="Button_Inserisci_Click" />
<br /><br />
<asp:Label ID="Label_Esito"
EnableViewState="false"
runat="server" Text="">
</asp:Label>
</div>
</form>
</body>
</html>
File Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.Collections;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
PopolaTabella();
}
private void PopolaTabella()
{
Class1 Dati = new Class1();
DataSet ds = Dati.ElencoAnagrafica();
GridView1.DataSource = ds;
GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
Class1 Dati = new Class1();
String nome = TextBox_Nome.Text;
String cognome = TextBox_Cognome.Text;
Int32 ris = Dati.ModificaRiga(
Convert.ToInt32(GridView1.SelectedDataKey.Value),
nome, cognome);
PopolaTabella();
}
protected void GridView1_SelectedIndexChanged(object sender, EventArgs e)
{
String keySelected = GridView1.SelectedDataKey.Value.ToString();
TextBox_Nome.Text =
Convert.ToString(GridView1.SelectedRow.Cells[1].Text);
TextBox_Cognome.Text =
Convert.ToString(GridView1.SelectedRow.Cells[2].Text);
}
protected void Button_Inserisci_Click(object sender, EventArgs e)
{
Class1 Dati = new Class1();
String nome = TextBoxNome_Ins.Text;
String cognome = TextBoxCognome_Ins.Text;
Int32 ris = Dati.InserisciRiga(nome, cognome);
if (ris > 0)
{
Label_Esito.Text = "Inserimento avvenuto con successo.
ID nuovo record = " + ris;
Label_Esito.ForeColor = System.Drawing.Color.Green;
}
else
{
Label_Esito.Text = "Errore";
Label_Esito.ForeColor = System.Drawing.Color.Red;
}
TextBoxNome_Ins.Text = "";
TextBoxCognome_Ins.Text = "";
PopolaTabella();
}
protected void GridView1_RowDeleting(object sender,
GridViewDeleteEventArgs e)
{
GridView1.SelectedIndex = e.RowIndex;
String key = GridView1.SelectedDataKey.Value.ToString();
GridView1.SelectedIndex = -1;
Class1 Dati = new Class1();
Int32 ris = Dati.CancellaRiga(key);
PopolaTabella();
//Queste funzionalità non funzionano senza sqldatasource
/*
string condizioneWhere = " ";
bool primaCondizione = true;
foreach (DictionaryEntry keyEntry in e.Keys)
{
if (!primaCondizione) condizioneWhere += " AND ";
condizioneWhere += keyEntry.Key + "=" + keyEntry.Value;
primaCondizione = false;
}
string logText = "";
foreach (DictionaryEntry valueEntry in e.Values)
{
logText += valueEntry.Key + "=" + valueEntry.Value + ";";
}
*/
}
}
File /App_Code/Class1.cs
using System;
using System.Data;
using System.Configuration;
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;
using System.Data.SqlClient;
public class Class1
{
private String connString = "";
private SqlConnection conn = new SqlConnection();
public Class1()
{
//stringa di connessione classica
/* connectionString="Data Source=192.168.1.1;
Initial Catalog=dati;
Persist Security Info=True;
User ID=xxxx;Password=xxxx" */
connString =
ConfigurationManager.
ConnectionStrings["DatabaseConnectionString2"].ConnectionString;
}
private void ApriConnessione()
{
conn.ConnectionString = connString;
if (conn.State != ConnectionState.Open)
{
conn.Open();
}
}
private void ChiudiConnessione()
{
if (conn.State != ConnectionState.Closed)
{
conn.Close();
}
}
private String Query(String nomeQuery)
{
String sQuery = "";
switch (nomeQuery)
{
case "ElencoAnagrafica":
sQuery = "SELECT [ID], [Nome], [Cognome] FROM [Anagrafica]";
break;
case "ModificaAnagrafica":
sQuery = "UPDATE Anagrafica
SET Nome = @Nome, Cognome = @Cognome WHERE ID = @ID";
break;
case "InserisciAnagrafica":
sQuery = "INSERT INTO Anagrafica(Nome,Cognome)
VALUES (@Nome,@Cognome);SELECT @ParamID = @@IDENTITY;";
break;
case "CancellaAnagrafica":
sQuery = "DELETE FROM Anagrafica WHERE ID = @ID";
break;
}
return sQuery;
}
public DataSet ElencoAnagrafica()
{
SqlCommand command = new SqlCommand();
SqlDataAdapter dataAdapter = new SqlDataAdapter();
DataSet ds = new DataSet();
ApriConnessione();
command.Connection = conn;
command.CommandText = Query("ElencoAnagrafica");
dataAdapter.SelectCommand = command;
dataAdapter.Fill(ds);
ChiudiConnessione();
return ds;
}
public int ModificaRiga(Int32 ID, String newNome, String newCognome)
{
ApriConnessione();
SqlCommand cmd = new SqlCommand(Query("ModificaAnagrafica"), conn);
cmd.Parameters.Add("@Nome", SqlDbType.VarChar, 50).Value = newNome;
cmd.Parameters.Add("@Cognome", SqlDbType.VarChar, 50).Value =
newCognome;
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
Int32 ris = cmd.ExecuteNonQuery();
ChiudiConnessione();
return ris;
}
public int InserisciRiga(String nome, String cognome)
{
ApriConnessione();
SqlCommand cmd = new SqlCommand(Query("InserisciAnagrafica"), conn);
cmd.Parameters.Add("@Nome", SqlDbType.VarChar, 50).Value = nome;
cmd.Parameters.Add("@Cognome", SqlDbType.VarChar, 50).Value = cognome;
SqlParameter paramID = new SqlParameter();
paramID.ParameterName = "@ParamID";
paramID.DbType = DbType.Int32;
paramID.Direction = ParameterDirection.Output;
cmd.Parameters.Add(paramID);
int result = 0;
result = cmd.ExecuteNonQuery();
if (result != 0)
{
Int32 ID = Convert.ToInt32(paramID.Value);
return ID;
}
return result;
}
public int CancellaRiga(String ID)
{
ApriConnessione();
SqlCommand cmd = new SqlCommand(Query("CancellaAnagrafica"), conn);
cmd.Parameters.Add("@ID", SqlDbType.Int).Value = ID;
Int32 ris = cmd.ExecuteNonQuery();
ChiudiConnessione();
return ris;
}
}
File
/App_Data/Database.mdf
File /web.config
<?xml version="1.0"?>
<configuration>
<appSettings/>
<connectionStrings>
<add name="DatabaseConnectionString2"
connectionString="Data Source=.\SQLEXPRESS;
AttachDbFilename=xxx;
Integrated Security=True;
Connect Timeout=30;
User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
.
.
.
|
|
|
|
|
|
|