non uso mai sql server.
Però ho fatto un esempietto funzionante che usa System.Data.SqlServerCe
La tabella, chiamata tbChiamante, ha un campo id, integer identity, RagSociale, nvarchar, e indirizzo, nvarchar. Sotto il codice
pagina
<%@ Page Language="VB" AutoEventWireup="false" CodeFile="a.aspx.vb" Inherits="prove_a" %>
<!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></title>
<link href="../App_Themes/classico10/StyleSheet.css" rel="stylesheet" type="text/css" />
</head>
<body>
<form id="form1" runat="server">
<div>
<label>Ragione sociale</label><br />
<asp:DropDownList ID="DropDownList1" runat="server">
<asp:ListItem></asp:ListItem>
<asp:ListItem Value="xxx" Text="xxx"></asp:ListItem>
<asp:ListItem Value="yyy" Text="yyy"></asp:ListItem>
<asp:ListItem Value="zzz" Text="zzz"></asp:ListItem>
</asp:DropDownList>
<br /><br /><br />
<label>Indirizzo</label><br />
<asp:TextBox ID="txtIndirizzo" runat="server"></asp:TextBox>
<br /><br /><br />
<asp:Button ID="SalvaDati" runat="server" Text="Button" />
<br /><br /><br />
<asp:Label ID="Label1" runat="server" Text="" EnableViewState="false"></asp:Label>
</div>
</form>
</body>
</html>
codice
Option Strict On
Imports o = System.Data.SqlServerCe
Partial Class prove_a
Inherits System.Web.UI.Page
Protected Sub SalvaDati_Click(sender As Object, e As System.EventArgs) Handles SalvaDati.Click
Dim StringaConnessione As String = "Data Source=C:\tmp\test.sdf"
Dim Connessione As o.SqlCeConnection = Nothing
Dim Comando As o.SqlCeCommand = Nothing
Dim Transazione As o.SqlCeTransaction = Nothing
Dim Sql As String = ""
Try
Connessione = New o.SqlCeConnection(StringaConnessione)
Connessione.Open()
Transazione = Connessione.BeginTransaction()
Comando = Connessione.CreateCommand()
Comando.Transaction = Transazione
Sql = "INSERT INTO tbChiamante (RagSociale, Indirizzo) VALUES (@RagSociale, @Indirizzo)"
Comando.CommandText = Sql
Comando.Parameters.Clear()
Comando.Parameters.Add(New o.SqlCeParameter() With {.ParameterName = "@RagSociale", .DbType = DbType.String, .Size = 100, .Value = StringNullToDBNull(Me.DropDownList1.SelectedItem.Text)})
Comando.Parameters.Add(New o.SqlCeParameter() With {.ParameterName = "@Indirizzo", .DbType = DbType.String, .Size = 100, .Value = StringNullToDBNull(Me.txtIndirizzo.Text)})
Comando.ExecuteNonQuery()
Transazione.Commit()
'Transazione.Rollback()
Me.Label1.Text = "Operazioni su database eseguite regolarmente"
Catch ex As Exception
Me.Label1.Text = HttpUtility.HtmlEncode(ex.Message).Replace(ControlChars.NewLine, "<br />").Replace(ControlChars.Lf, "<br />").Replace(ControlChars.Cr, "<br />")
Finally
If (Not (Connessione Is Nothing)) Then Connessione.Close()
End Try
End Sub
'--------------------------------------------------------
'Converte una stringa nulla in DBNull
'--------------------------------------------------------
Public Function StringNullToDBNull(ByVal s As String) As Object
If String.IsNullOrWhiteSpace(s) Then
Return DBNull.Value
Else
Return s.Trim
End If
End Function
End Class