Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations SkipVought on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

saving records with ASP.Net 1

Status
Not open for further replies.

technisup

Programmer
Sep 28, 2007
41
0
0
US
Hello, i need some help because i'm new at this and i'd like to save a record with Asp.Net and visual web developer from a form. I really thank any support that you can bring me.

 
Welcome to dot net development, its fun, and unlimited, however you will have some learning to do.

You got in at a good time, because VWD has many drag and drop components to make development quicker. You should spend some time figuring out why it works when you are done, and will be amazed how much you can do.

Start out reading 4guysfromrolla, many to the point examples that can be done in VWD.


Just start attempting to develop, and when you run into snags or questions, this is a great place to ask questions. Be sure to post source code where you are stuck at, it really helps get your question answered quicker.
 
Also welcome!

This is a wonderful resource also.
There are video tutorials that you can watch on almost anything about development in .NET.

There is no right or wrong way to do things as long as you learn.
 
Thank you so much to both of you.

I'm following now both, but now i have a new problem, why my formview is not displayed at my webpage?

This is the code:

<%@ Page Language="VB" MasterPageFile="~/MasterPage.master" Title="Untitled Page" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder2" Runat="Server">
<br />
<span style="font-size: 24pt">Usuarios<br />
<br />
<span style="font-size: 11pt; font-family: Tahoma">Por favor ingrese los datos solicitados
en el formulario.<br />
<br />
<br />
<br />
<asp:FormView ID="FormView1" runat="server" DataKeyNames="IdUsuario" DataSourceID="SqlDataSource1"
Style="position: relative">
<EditItemTemplate>
IdUsuario:
<asp:Label ID="IdUsuarioLabel1" runat="server" Text='<%# Eval("IdUsuario") %>'></asp:Label><br />
Usuario:
<asp:TextBox ID="UsuarioTextBox" runat="server" Text='<%# Bind("Usuario") %>'>
</asp:TextBox><br />
Clave:
<asp:TextBox ID="ClaveTextBox" runat="server" Text='<%# Bind("Clave") %>'>
</asp:TextBox><br />
Nombre:
<asp:TextBox ID="NombreTextBox" runat="server" Text='<%# Bind("Nombre") %>'>
</asp:TextBox><br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>'>
</asp:TextBox><br />
IdPerfil:
<asp:TextBox ID="IdPerfilTextBox" runat="server" Text='<%# Bind("IdPerfil") %>'>
</asp:TextBox><br />
IdGrupo:
<asp:TextBox ID="IdGrupoTextBox" runat="server" Text='<%# Bind("IdGrupo") %>'>
</asp:TextBox><br />
IdCiudad:
<asp:TextBox ID="IdCiudadTextBox" runat="server" Text='<%# Bind("IdCiudad") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="UpdateButton" runat="server" CausesValidation="True" CommandName="Update"
Text="Update">
</asp:LinkButton>
<asp:LinkButton ID="UpdateCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</EditItemTemplate>
<InsertItemTemplate>
Usuario:
<asp:TextBox ID="UsuarioTextBox" runat="server" Text='<%# Bind("Usuario") %>'>
</asp:TextBox><br />
Clave:
<asp:TextBox ID="ClaveTextBox" runat="server" Text='<%# Bind("Clave") %>'>
</asp:TextBox><br />
Nombre:
<asp:TextBox ID="NombreTextBox" runat="server" Text='<%# Bind("Nombre") %>'>
</asp:TextBox><br />
Email:
<asp:TextBox ID="EmailTextBox" runat="server" Text='<%# Bind("Email") %>'>
</asp:TextBox><br />
Perfil:
<asp:DropDownList ID="DropDownList1" runat="server" DataSourceID="SqlDataSource1"
DataTextField="Descripcion" DataValueField="IdPerfil" SelectedValue='<%# Bind("IdPerfil") %>'
Style="position: relative">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:SIPConnectionString %>"
SelectCommand="SELECT * FROM [Perfil]"></asp:SqlDataSource>
<br />
Grupol:
<asp:DropDownList ID="DropDownList2" runat="server" DataSourceID="SqlDataSource2"
DataTextField="Descripcion" DataValueField="IdGrupo" Style="position: relative">
</asp:DropDownList><asp:SqlDataSource ID="SqlDataSource2" runat="server" ConnectionString="<%$ ConnectionStrings:SIPConnectionString %>"
SelectCommand="SELECT * FROM [Grupo]"></asp:SqlDataSource>
<br />
IdGrupo:
<asp:TextBox ID="IdGrupoTextBox" runat="server" Text='<%# Bind("IdGrupo") %>'>
</asp:TextBox><br />
IdCiudad:
<asp:TextBox ID="IdCiudadTextBox" runat="server" Text='<%# Bind("IdCiudad") %>'>
</asp:TextBox><br />
<asp:LinkButton ID="InsertButton" runat="server" CausesValidation="True" CommandName="Insert"
Text="Insert">
</asp:LinkButton>
<asp:LinkButton ID="InsertCancelButton" runat="server" CausesValidation="False" CommandName="Cancel"
Text="Cancel">
</asp:LinkButton>
</InsertItemTemplate>
<ItemTemplate>
IdUsuario:
<asp:Label ID="IdUsuarioLabel" runat="server" Text='<%# Eval("IdUsuario") %>'></asp:Label><br />
Usuario:
<asp:Label ID="UsuarioLabel" runat="server" Text='<%# Bind("Usuario") %>'></asp:Label><br />
Clave:
<asp:Label ID="ClaveLabel" runat="server" Text='<%# Bind("Clave") %>'></asp:Label><br />
Nombre:
<asp:Label ID="NombreLabel" runat="server" Text='<%# Bind("Nombre") %>'></asp:Label><br />
Email:
<asp:Label ID="EmailLabel" runat="server" Text='<%# Bind("Email") %>'></asp:Label><br />
IdPerfil:
<asp:Label ID="IdPerfilLabel" runat="server" Text='<%# Bind("IdPerfil") %>'></asp:Label><br />
IdGrupo:
<asp:Label ID="IdGrupoLabel" runat="server" Text='<%# Bind("IdGrupo") %>'></asp:Label><br />
IdCiudad:
<asp:Label ID="IdCiudadLabel" runat="server" Text='<%# Bind("IdCiudad") %>'></asp:Label><br />
<asp:LinkButton ID="EditButton" runat="server" CausesValidation="False" CommandName="Edit"
Text="Edit">
</asp:LinkButton>
<asp:LinkButton ID="DeleteButton" runat="server" CausesValidation="False" CommandName="Delete"
Text="Delete">
</asp:LinkButton>
<asp:LinkButton ID="NewButton" runat="server" CausesValidation="False" CommandName="New"
Text="New">
</asp:LinkButton>
</ItemTemplate>
</asp:FormView>
<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConflictDetection="CompareAllValues"
ConnectionString="<%$ ConnectionStrings:SIPConnectionString %>" DeleteCommand="DELETE FROM [Usuario] WHERE [IdUsuario] = @original_IdUsuario AND [Usuario] = @original_Usuario AND [Clave] = @original_Clave AND [Nombre] = @original_Nombre AND = @original_Email AND [IdPerfil] = @original_IdPerfil AND [IdGrupo] = @original_IdGrupo AND [IdCiudad] = @original_IdCiudad"
InsertCommand="INSERT INTO [Usuario] ([Usuario], [Clave], [Nombre], [Email], [IdPerfil], [IdGrupo], [IdCiudad]) VALUES (@Usuario, @Clave, @Nombre, @Email, @IdPerfil, @IdGrupo, @IdCiudad)"
OldValuesParameterFormatString="original_{0}" SelectCommand="SELECT * FROM [Usuario]"
UpdateCommand="UPDATE [Usuario] SET [Usuario] = @Usuario, [Clave] = @Clave, [Nombre] = @Nombre, [Email] = @Email, [IdPerfil] = @IdPerfil, [IdGrupo] = @IdGrupo, [IdCiudad] = @IdCiudad WHERE [IdUsuario] = @original_IdUsuario AND [Usuario] = @original_Usuario AND [Clave] = @original_Clave AND [Nombre] = @original_Nombre AND [Email] = @original_Email AND [IdPerfil] = @original_IdPerfil AND [IdGrupo] = @original_IdGrupo AND [IdCiudad] = @original_IdCiudad">
<DeleteParameters>
<asp:parameter Name="original_IdUsuario" Type="Int16" />
<asp:parameter Name="original_Usuario" Type="String" />
<asp:parameter Name="original_Clave" Type="String" />
<asp:parameter Name="original_Nombre" Type="String" />
<asp:parameter Name="original_Email" Type="String" />
<asp:parameter Name="original_IdPerfil" Type="Int16" />
<asp:parameter Name="original_IdGrupo" Type="Int16" />
<asp:parameter Name="original_IdCiudad" Type="Int16" />
</DeleteParameters>
<UpdateParameters>
<asp:parameter Name="Usuario" Type="String" />
<asp:parameter Name="Clave" Type="String" />
<asp:parameter Name="Nombre" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="IdPerfil" Type="Int16" />
<asp:parameter Name="IdGrupo" Type="Int16" />
<asp:parameter Name="IdCiudad" Type="Int16" />
<asp:parameter Name="original_IdUsuario" Type="Int16" />
<asp:parameter Name="original_Usuario" Type="String" />
<asp:parameter Name="original_Clave" Type="String" />
<asp:parameter Name="original_Nombre" Type="String" />
<asp:parameter Name="original_Email" Type="String" />
<asp:parameter Name="original_IdPerfil" Type="Int16" />
<asp:parameter Name="original_IdGrupo" Type="Int16" />
<asp:parameter Name="original_IdCiudad" Type="Int16" />
</UpdateParameters>
<InsertParameters>
<asp:parameter Name="Usuario" Type="String" />
<asp:parameter Name="Clave" Type="String" />
<asp:parameter Name="Nombre" Type="String" />
<asp:parameter Name="Email" Type="String" />
<asp:parameter Name="IdPerfil" Type="Int16" />
<asp:parameter Name="IdGrupo" Type="Int16" />
<asp:parameter Name="IdCiudad" Type="Int16" />
</InsertParameters>
</asp:SqlDataSource>
<br />
&nbsp;<br />
&nbsp;<br />
&nbsp;
</span></span>
</asp:Content>

Thx in advance for the help.
 
I've noticed that the table involved here, was empty, but shouldn't the system check this?

 
I would make 1 recommendation. Never use a DataSource control. Of any kind. there are a number of reasons for this. The most relevant: it's near impossible to debug. There are other reasons which deal more with programming principles and layering the application.

There is no right or wrong way to do things as long as you learn.
In a practical sense yes, but this is dangerous. anyone can learn to drive just by getting in a car. You don't want to be anywhere near them when they do.

If you learn to use the tools before you jump right in it's much easier to understand the why/how of .net.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
So, are you suggesting to type my code like in ASP 3.0?

If so, can you provide me some example?
Thx
 
no, i'm saying you should avoid web controls that do not pertain to html.

there are plenty of examples on the web on how to use ADO.Net objects. in it's simplest form it looks like this
Code:
Presenter presenter = new Presenter();
MyGridView.DataSource = presenter.GetData(1);
MyGridview.DataBind();

Code:
public class Presenter
{
   public IEnumerable GetData(int id)
   {
      DataTable results = new DataTable();
      ConnectionStringSetting settings = ConfigurationManager.ConnectionStrings["key"];
      using(IDbConnection connection = DbProviderFactory.CreateConnection(settings.Provider))
      {
         connection.ConnectionString = settings.ConnectionString;
         connection.Open();
         using(IDbCommand command = connection.CreateCommand)
         {
            command.CommandText = "select [fields,] from [table] where [column] = @value";
            IDbParameter parameter = command.CreateParameter("value");
            parameter.Value = id;
            command.Parameters.Add(parameter);

            results.Load(command.ExecuteReader());
         }
      }
      return results;
   }
}
this can be refactored into more elaborate objects. check out the "Session(Connection) Per View" pattern. basically you have an object which manages a database connection per request. this reduces the amount of code you have to write each time you want to query the database. I would consider this a moderate to advanced concept. if you're just starting out this may be overwhelming. If it's not, then that's even better.

I also recommend researching the S.O.L.I.D. design principles. These are the core of any OOP language.

There is a movement back to "spaghetti" code for rendering html. however the code deals with html only. there is no data access or business logic being preformed.

Jason Meckley
Programmer
Specialty Bakers, Inc.
 
Thanks for the help. The solution was related with the SQL server, i had it in Windows Authentication mode only so I switch it to Windows and SQL authentication and change the connection string and works.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top