Wrox Programmer Forums
|
ASP.NET 2.0 Basics If you are new to ASP or ASP.NET programming with version 2.0, this is the forum to begin asking questions. Please also see the Visual Web Developer 2005 forum.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the ASP.NET 2.0 Basics 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 July 21st, 2007, 05:12 PM
Authorized User
 
Join Date: Apr 2006
Posts: 35
Thanks: 0
Thanked 0 Times in 0 Posts
Default How can I UPDATE database?

The code below works (much help from forum) but I don't know what to write in the Button2_Click for it to UPDATE the database with the "L_Rank" value changes made in the foreach loop. I still do not really understand why I could not call the ds.Table["Random"] and had to create the 'dt' object.

I created the DataTable 'dt' in the Button1_Click event so I could modify the L-rank values. When I tried to use ds.Tables["Random001"] inside the Button1_Click event, kept getting 'object not found error. I do not yet understand object instantiation. When DataTable 'dt' is created is it a mirror of the "Random001"? And after the loop runs are the L_Rank values in ds.Table["Random001"] also changed?

I guess what I am asking is if both ds.Table["Random001"] and DataTable 'dt' exist at the same time or is ds.Table["Random001"] destroyed when DataTable dt is created? Can someone give a better understanding of what actually happens? - thank you,



 public DataTable GetRandLinks()
    {
        SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrin gs["homeexpoConnectionStringA"].ConnectionString);
        SqlCommand cmd = new SqlCommand("RandomizerSelect001", con);
        cmd.CommandType = CommandType.StoredProcedure;
        SqlDataAdapter da = new SqlDataAdapter();
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        try
        {
            da.Fill(ds, "Random001");
            return ds.Tables["Random001"];
        }
        catch
        { throw new ApplicationException("Data error"); }
    }
    // Part Two write a random value into L_rank for each row
    protected void Button1_Click(object sender, EventArgs e)
    {
        GetRandLinks();
        DataTable dt = GetRandLinks();
        int RowIncrement;
        RowIncrement = 0;
        System.Random myRandom = new System.Random();
        foreach (DataRow row in dt.Rows)
        {
            int LinkRank = myRandom.Next(25, 250);
            row["L_Rank"] = LinkRank;
            RowIncrement++;
        }
        GridView1.DataSource = dt;
        GridView1.DataMember = "Random001";
        GridView1.DataBind();
    }
    protected void Button2_Click(object sender, EventArgs e)
    {
      /* // NEED TO WRITE L_Rank changed values back to SQL Server

              this is the stored procedure that I used in my old 1.0 ASP code:
             PROCEDURE RandomizeLinks
             @L_ID int,
             @L_Rank int
             AS
             UPDATE tblLinkInfo_OLD2
             SET L_Rank = @L_Rank
             WHERE (L_ID = @L_ID)
     */
    }


 
Old July 25th, 2007, 03:24 PM
Friend of Wrox
 
Join Date: Jun 2007
Posts: 477
Thanks: 10
Thanked 19 Times in 18 Posts
Default

OK, Databases are nasty but I can usually puzzle my way through and here's how I interpret your trouble with instantiation.
Code:
DataSet ds = new DataSet();
This is actually two expressions condensed into one. I'm so used to it I can't remember the separated format but basically
Code:
DataSet ds
Is declaring a variable, it's saying that ds is a new variable of type DataSet. At THIS point there IS no DataSet, simply a variable waiting to have a REAL DataSet put into it. The second part...
Code:
ds = new DataSet();
uses the NEW keyword to create a REAL DataSet. ds = then assigns the new DataSet to ds. NOW ds has a DataSet sitting inside it that you can program against. It sounds like you're at least partly OK with this.

Here's the part you said you're confused about. A DataSet has it's own internal structure, in fact it's set up very much like a Database. You've recognized that DataSets have a table(s) inside them, in fact for a moment in the code you even get to "see" them.
Code:
            da.Fill(ds, "Random001");
            return ds.Tables["Random001"];
It looks like they're sitting there for you to use, right? I admit I don't get how ASP.NET lets you make that call when it obviously won't let you use it the way you want to. (There must be some data type conversioning going on from the ds.Table to a full DataTable type, at least that's my best guess)

The upshot is that the table has to be declared explicitly. The giveaway is the error message you get. When you try to access the table using ds.Table it tells you "Hey, I can't find anything." Once you explicitly declare the DataTable...
Code:
DataTable dt = GetRandLinks();
it's smooth sailing. Obviously the part which directly addresses your error is
Code:
dt = GetRandLinks();
That's the part which fills your DataTable variable with the actual table. Hope that part helps anyway.
:)

-------------------------

Whatever you can do or dream you can, begin it. Boldness has genius, power and magic in it. Begin it now.
-Johann von Goethe

When Two Hearts Race... Both Win.
-Dove Chocolate Wrapper

Chroniclemaster1, Founder of www.EarthChronicle.com
A Growing History of our Planet, by our Planet, for our Planet.





Similar Threads
Thread Thread Starter Forum Replies Last Post
update database with checkbox mike7510uk ASP.NET 2.0 Basics 1 December 12th, 2006 07:10 AM
Why can I not update my database? fazzou Access 5 November 16th, 2006 11:13 AM
code doesn't update database Lesviper Beginning VB 6 1 February 7th, 2005 10:18 AM
Trying to update to database Calibus Classic ASP Databases 20 July 23rd, 2004 03:28 PM
database update joshil Access ASP 2 May 24th, 2004 04:22 AM





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