Wrox Programmer Forums
|
BOOK: Beginning ASP.NET 4 : in C# and VB
This is the forum to discuss the Wrox book Beginning ASP.NET 4: in C# and VB by Imar Spaanjaars; ISBN: 9780470502211
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning ASP.NET 4 : in C# and VB section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
 
Old February 12th, 2014, 12:08 PM
Authorized User
 
Join Date: Feb 2014
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to use data adapters in .net

Im trying to make a duplicate page of the Genres.aspx page in the Planet Wrox site from the Management folder, where we are able to Update, Delete, and Insert into the Genres table.

I have the Html set up the same way as the Genres.aspx except without the sqlDataSource of course.

My question is to which event do I connect each operation to, and how to do them

my select statement for outputting is this:

SqlConnection myConnection = new SqlConnection();
SqlCommand myCommand = new SqlCommand();
SqlDataAdapter myAdapter = new SqlDataAdapter();
DataTable myTable = new DataTable();

myConnection.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;
myCommand.CommandText = "SELECT * FROM Genre";
myCommand.CommandType = CommandType.Text;
myCommand.Connection = myConnection;
myAdapter.SelectCommand = myCommand;
myAdapter.Fill(myTable);

GridView1.DataSource = myTable.DefaultView;
GridView1.DataBind();

myAdapter.Dispose();
myCommand.Dispose();
myConnection.Dispose();
 
Old February 15th, 2014, 10:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

You can hook into events such as Inserting and Updating to set the relevant data. Alternatively, you can look into Model Binding: http://blogs.msdn.com/b/webdev/archi...web-forms.aspx (which requires recent versions of .NET and Visual Studio).

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 16th, 2014, 05:30 PM
Authorized User
 
Join Date: Feb 2014
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default How to affect the db through the function calls

I understand that my question how does the event handler need to be written I had a sql data adapter built with the event handlers with the following code:

public SqlDataAdapter crud()
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;
SqlDataAdapter adapt = new SqlDataAdapter();
SqlCommand command = new SqlCommand();

command = new SqlCommand("INSERT INTO Genre ([Name], [SortOrder]) VALUES ( @Name, @SortOrder)", connect);
command.Parameters.Add("@Name", SqlDbType.NVarChar, 200, "Name");
command.Parameters.Add("@SortOrder", SqlDbType.Int, 32, "SortOrder");
adapt.InsertCommand = command;

command = new SqlCommand("UPDATE Genre SET Name = @Name, SortOrder = @SortOrder WHERE Id = @oldId", connect);
command.Parameters.Add("@Id", SqlDbType.Int, 32, "Id");
command.Parameters.Add("@Name", SqlDbType.NVarChar, 200, "Name");
command.Parameters.Add("@SortOrder", SqlDbType.Int, 32, "SortOrder");
SqlParameter para = command.Parameters.Add("@oldId", SqlDbType.Int, 32, "Id");
para.SourceVersion = DataRowVersion.Original;
adapt.UpdateCommand = command;

command = new SqlCommand("DELETE FROM Genre WHERE Id = @Id", connect);
para = command.Parameters.Add("@Id", SqlDbType.Int, 32, "Id");
para.SourceVersion = DataRowVersion.Original;
adapt.DeleteCommand = command;

return adapt;
}

protected void DetailsView1_ItemInserting(object sender, DetailsViewInsertEventArgs e)
{
string comstr = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;
using (SqlConnection con = new SqlConnection(comstr))
{
using (SqlCommand cmd = new SqlCommand())
{

}
}

function1();
}
protected void GridView1_RowEditing(object sender, GridViewEditEventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;


}
protected void GridView1_RowUpdating(object sender, GridViewUpdateEventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;


}
protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;


}
protected void GridView1_RowDeleting(object sender, GridViewDeleteEventArgs e)
{
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;


}
 
Old February 17th, 2014, 04:47 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

I am not sure I understand what you are asking then. Can you elaborate? Did you look into model binding?

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 17th, 2014, 11:15 AM
Authorized User
 
Join Date: Feb 2014
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default Looked over Model binding

I did, though they don't use data adapters explicitly, but the model bind is similar to data bind and instead create data sets in the namespace (that is what DbSet is right?). So then what I want to do is bind data to the data set manipulate through the UI I have set up then push those changes from the data set made onto the actual database, correct?
 
Old February 17th, 2014, 06:15 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Yes, that's correct.

Any reason you are using DataSets? They are considered a dated concept, and there are much better and easier alternatives.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 17th, 2014, 06:19 PM
Authorized User
 
Join Date: Feb 2014
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default data set vs data table

would it be better just to use a data table instead of a data set because im only manipulating one table in the database or would I need a data set for different instance of the data table?
 
Old February 17th, 2014, 06:25 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Same thing; a DataSet represents the database, and the DataTable represents a table. If I recall correctly, you always have to submit changes to a table back in the parent DataSet.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!
 
Old February 18th, 2014, 12:01 PM
Authorized User
 
Join Date: Feb 2014
Posts: 13
Thanks: 0
Thanked 0 Times in 0 Posts
Default GridView not updating

I have the logic all set up in the code behind file but everytime I click insert in on the details view, the grid view refresh doesn't refresh with the new insert value. Am I missing something?

Details view onItemInserting is set to function2,

protected void function2(object sender, EventArgs e)
{
SqlDataAdapter adapt = new SqlDataAdapter();
SqlConnection connect = new SqlConnection();
connect.ConnectionString = ConfigurationManager.ConnectionStrings["PlanetWroxConnectionString1"].ConnectionString;
SqlCommand command = new SqlCommand();
DataTable table = new DataTable();

command = new SqlCommand("INSERT INTO Genre (Name, SortOrder) VALUES (@Name, @SortOrder)");
//command.Parameters.Add("@Id",SqlDbType.Int,32,"Id" );
command.Parameters.Add("@Name", SqlDbType.NVarChar, 200, "Name");
command.Parameters.Add("@SortOrder", SqlDbType.Int, 32, "SortOrder");
command.Connection = connect;

adapt.InsertCommand = command;
//adapt.InsertCommand.ExecuteNonQuery();
command = new SqlCommand("SELECT * FROM Genre", connect);
adapt.SelectCommand = command;
adapt.Fill(table);

GridView1.DataSource = table.DefaultView;
GridView1.DataBind();

adapt.Dispose();
command.Dispose();
connect.Dispose();
}
 
Old February 19th, 2014, 04:10 AM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

Hi there,

I don't see where you are getting the values from the form controls, and I don't see you execute the query so I am not surprised this doesn't work. All you do is set up a command, and then execute the select. Where does this code come from? You may need to find a better Insert example that does the parameter-based insert.

I'll try it again: any reason you are using DataSets? They are considered dated and there are much better alternatives.

Cheers,

Imar
__________________
Imar Spaanjaars
http://Imar.Spaanjaars.Com
Follow me on Twitter

Author of Beginning ASP.NET 4.5 : in C# and VB, Beginning ASP.NET Web Pages with WebMatrix
and Beginning ASP.NET 4 : in C# and VB.
Did this post help you? Click the button below this post to show your appreciation!





Similar Threads
Thread Thread Starter Forum Replies Last Post
Installing the Control Adapters Randy-s BOOK: Professional ASP.NET 2.0 Design: CSS, Themes, and Master Pages ISBN: 978-0-470-12448-2 5 June 26th, 2009 07:59 AM
CSS Friendly control adapters Will BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 March 11th, 2009 06:15 AM
ASP.NET CSS Friendly Control Adapters VeganMan BOOK: Beginning ASP.NET 3.5 : in C# and VB BOOK ISBN: 978-0-470-18759-3 2 April 8th, 2008 09:21 PM
Table Adapters mfouchi BOOK: ASP.NET 2.0 Instant Results ISBN: 978-0-471-74951-6 3 September 23rd, 2006 05:16 AM
.NET Data Provider pradeep_itguy ADO.NET 0 October 3rd, 2004 04:03 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.