Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Making a dataset update

Posted on 2006-06-04
4
Medium Priority
?
259 Views
Last Modified: 2006-11-18
I have written this code:

  protected void cmdUpdateProfile_Click(object sender, EventArgs e)
    {
        string strConnectUserInfo;
        string UserName = User.Identity.Name;
        DataSet dsUserInfo = new DataSet();//makes the dataset
        strConnectUserInfo = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\SnakeDB.mdf;Integrated Security=True;User Instance=True";
        SqlConnection mycon = new SqlConnection(strConnectUserInfo);//makes a new connection using the strConnect connection string
        SqlDataAdapter objDa = new SqlDataAdapter("Update UserPost SET Fname = '" + txtFname.Text + "',Lname = '" + txtLname.Text + "', Email = '" + txtEmail.Text + "', Interests = '" + txtInterests.Text + "', AIM = '" + txtAIM.Text + "',Phone = '" + txtPhone.Text + "' WHERE ScreenName = '" + UserName + "'", mycon);
        objDa.Fill(dsUserInfo);
    }


There is also code to populate the values in the page load:

 protected void Page_Load(object sender, EventArgs e)
    {
        lblUserName.Text = User.Identity.Name;

        //gets the information from the users table
        string strConnectUserInfo;
        DataSet dsUserInfo = new DataSet();//makes the dataset
        strConnectUserInfo = "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\SnakeDB.mdf;Integrated Security=True;User Instance=True";
        SqlConnection mycon = new SqlConnection(strConnectUserInfo);//makes a new connection using the strConnect connection string
        SqlDataAdapter objDa = new SqlDataAdapter("SELECT * FROM UserPost WHERE ScreenName = '" + Convert.ToString(User.Identity.Name) + "'", mycon);
        objDa.Fill(dsUserInfo);

        try
        {
            txtFname.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["Fname"]);
            txtLname.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["Lname"]);
            txtEmail.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["Email"]);
            txtAIM.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["AIM"]);
            txtPhone.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["Phone"]);
            txtInterests.Text = Convert.ToString(dsUserInfo.Tables[0].Rows[0]["Interests"]);
        }
        catch
        {
        }

The problem is that the data will not update when the Submit button is clicked.  How can I make this work?
0
Comment
Question by:DImhoff1
  • 2
  • 2
4 Comments
 
LVL 6

Accepted Solution

by:
nehaya earned 2000 total points
ID: 16827993
SqlDataAdapter objDa = new SqlDataAdapter("Update UserPost SET Fname = '" + txtFname.Text + "',Lname = '" + txtLname.Text + "', Email = '" + txtEmail.Text + "', Interests = '" + txtInterests.Text + "', AIM = '" + txtAIM.Text + "',Phone = '" + txtPhone.Text + "' WHERE ScreenName = '" + UserName + "'", mycon);
        objDa.Fill(dsUserInfo);

here you wrote UPDATE command,
but actullay, it's for SELECT!
to set up ur update command do like this:

        SqlDataAdapter objDa = new SqlDataAdabpter();
        System.Data.SqlClient.SqlCommand updateCommand = new System.Data.SqlClient.SqlCommand();
        updateCommand.CommandText = "UPDATE COMMAND GOES HERE";
        updateCommand.Connection = mycon;
        objDa.UpdateCommand = updateCommand;
        objDa.Fill(dsUserInfo);
0
 

Author Comment

by:DImhoff1
ID: 16828078
What makes the code I wrote for SELECT?  I am pretty new to asp.net and I want to avoid this in the future.  Also, should I write all SqlCommands like your code (is there a SELECT method for the sqldataadapter object as well).  I would just check but I wont be at that computer for a while.
0
 
LVL 6

Expert Comment

by:nehaya
ID: 16828190
The constuctor you use, is like this
SqlDataAdapter(string selectCommand, SqlConnection connection)

to understand it more,
write:
SqlDataAdapter obj;
and then write this:
obj = new SqlDataSource( // here you will see the constructor methods popup, and you choosed the third one in your code above
so, if you did like this
SqlDataAdapter obj = new SqlDataSource();
you have to set the commands for Select,Update,Delete and Insert, and it goes like this:
obj.SelectCommad = mySelectCommand;
and same to all opertaions..
however, you don't have to set all operation, just set what ever you need.
so, since most of operations are for select, they made it easy on you by setting a constructor for Select statment!

==should I write all SqlCommands like your code (is there a SELECT method for the sqldataadapter object as well
you can write them in the same way ,, and do only what you need..
SELECT can be set in the same way, AND it can be set using the constructor(just SELECT operation)
0
 

Author Comment

by:DImhoff1
ID: 16829332
Thank you for all of the extra help, I understand now.
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
In an ASP.NET application, I faced some technical problems. In this article, I list them out and show the solutions that I found.  I hope it will be useful. Problem: After closing a pop-up window, the parent page should be refreshed automaticall…
Are you ready to place your question in front of subject-matter experts for more timely responses? With the release of Priority Question, Premium Members, Team Accounts and Qualified Experts can now identify the emergent level of their issue, signal…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses

810 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question