0

Hi,
I'm trying to update the user information from textbox
when the user click view my account information in the main page it will take them to another page which will display their account information they will be able to change everything excpet for the username becouse it is primary key, I don't have gridview I just want it to update the data in the database

here is what I did

<asp:SqlDataSource ID="SqlDataSource1" runat="server" ConnectionString="<%$ ConnectionStrings:StringConnection %>" DeleteCommand="DELETE FROM [accountInfo] WHERE [username] = @username" InsertCommand="INSERT INTO [accountInfo] ([username], [firstName], [lastName], [DOB], [email], [password], [creditCardNameHolder], [creditCardNumber], [country], [city], [zipCode], [mobileNo]) VALUES (@username, @firstName, @lastName, @DOB, @email, @password, @creditCardNameHolder, @creditCardNumber, @country, @city, @zipCode, @mobileNo)" ProviderName="<%$ ConnectionStrings:FlightConnection.ProviderName %>" SelectCommand="SELECT * FROM [accountInfo]" UpdateCommand="UPDATE [accountInfo] SET [firstName] = @firstName, [lastName] = @lastName, [DOB] = @DOB, [email] = @email, [password] = @password, [creditCardNameHolder] = @creditCardNameHolder, [creditCardNumber] = @creditCardNumber, [country] = @country, [city] = @city, [zipCode] = @zipCode, [mobileNo] = @mobileNo WHERE [username] = @username">
                            <DeleteParameters>
                                <asp:Parameter Name="username" Type="String" />
                            </DeleteParameters>
                            <InsertParameters>
                                <asp:Parameter Name="username" Type="String" />
                                <asp:Parameter Name="firstName" Type="String" />
                                <asp:Parameter Name="lastName" Type="String" />
                                <asp:Parameter Name="DOB" Type="String" />
                                <asp:Parameter Name="email" Type="String" />
                                <asp:Parameter Name="password" Type="String" />
                                <asp:Parameter Name="creditCardNameHolder" Type="String" />
                                <asp:Parameter Name="creditCardNumber" Type="String" />
                                <asp:Parameter Name="country" Type="String" />
                                <asp:Parameter Name="city" Type="String" />
                                <asp:Parameter Name="zipCode" Type="String" />
                                <asp:Parameter Name="mobileNo" Type="String" />
                            </InsertParameters>
                            <UpdateParameters>
                                <asp:Parameter Name="firstName" Type="String" />
                                <asp:Parameter Name="lastName" Type="String" />
                                <asp:Parameter Name="DOB" Type="String" />
                                <asp:Parameter Name="email" Type="String" />
                                <asp:Parameter Name="password" Type="String" />
                                <asp:Parameter Name="creditCardNameHolder" Type="String" />
                                <asp:Parameter Name="creditCardNumber" Type="String" />
                                <asp:Parameter Name="country" Type="String" />
                                <asp:Parameter Name="city" Type="String" />
                                <asp:Parameter Name="zipCode" Type="String" />
                                <asp:Parameter Name="mobileNo" Type="String" />
                                <asp:Parameter Name="username" Type="String" />
                            </UpdateParameters>
                        </asp:SqlDataSource>

...

        protected void btnUpdate_Click(object sender, EventArgs e)
        {
            string conString = ConfigurationManager.ConnectionStrings["StringConnection"].ConnectionString;

            SqlCeConnection con = new SqlCeConnection(conString);

            string fName = TextBoxFName.Text.Trim();  
            string lName = TextBoxLName.Text.Trim();
            string city =TextBoxCity.Text.Trim();    
             string email = TextBoxEmail.Text.Trim();  
            string pass =TextBoxPassword.Text.Trim();
            string username = lblUserName.Text.Trim();

           string nameCC =  TextBoxNameCreditCard.Text.Trim();  
            string ccNo= TextBoxCrediCardNo.Text.Trim();  
            string country = TextBoxCountry.Text.Trim();   

            string zipCode = TextBoxZipCpde.Text.Trim();
            string mobileNo = TextBoxMobileNo.Text.Trim();


           string sql = "UPDATE accountInfo SET firstName = @fName,  lastName = @lName, email = @email, password = @pass, creditCardNameHolder = @nameCC, [creditCardNumber] = @ccNo, city = @city, country = @country, zipCode = @zipCode, mobileNo = @mobileNo WHERE username =@username";
           SqlCeCommand Updatecmd = new SqlCeCommand(sql, con);
           Updatecmd.Parameters.AddWithValue("@fName", fName);
           Updatecmd.Parameters.Add("@username", SqlDbType.NChar);
           Updatecmd.Parameters.AddWithValue("@lName", lName);
           Updatecmd.Parameters.AddWithValue("@email", email);
           Updatecmd.Parameters.AddWithValue("@city", city);
           Updatecmd.Parameters.AddWithValue("@pass", pass);
           Updatecmd.Parameters.AddWithValue("@nameCC", nameCC);
           Updatecmd.Parameters.AddWithValue("@ccNo", ccNo);
           Updatecmd.Parameters.AddWithValue("@country", country);
           Updatecmd.Parameters.AddWithValue("@zipCode", zipCode);
           Updatecmd.Parameters.AddWithValue("@mobileNo", mobileNo);
          // Updatecmd.Parameters.Add("@fName",SqlDbType.NVarChar).Value = fName;

           try
           {
               con.Open();

               Updatecmd.ExecuteNonQuery();
               con.Close();
           }
           catch (Exception ex)
           {
           }



        }

but I don't know why it not working

Edited by sk8ergirl

2
Contributors
1
Reply
18
Views
4 Years
Discussion Span
Last Post by LastMitch
0

but I don't know why it not working

The code you provided looks incomplete. There should be another function called:

protected void Page_Load(object sender, EventArgs e){};

This looks wrong to me:

Updatecmd.Parameters.Add("@username", SqlDbType.NChar);

Instead of this:

Updatecmd.Parameters.Add("@username", SqlDbType.NChar);

It should look like this:

Updatecmd.Parameters.AddWithValue("@username", username);

The reason is because you establish the username as

string username = lblUserName.Text.Trim();

So it should be username not SqlDbType.NChar:

Updatecmd.Parameters.AddWithValue("@username", username);

You have alot of minor errors.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.