ASP.NET how to save textbox in database?

Posted by mahsoon on Stack Overflow See other posts from Stack Overflow or by mahsoon
Published on 2013-10-26T08:13:10Z Indexed on 2013/10/26 9:54 UTC
Read the original article Hit count: 276

Filed under:
|
|

I used some textboxes to get some info from users + a sqldatasource

    <tr>
        <td class="style3" colspan="3" 
            style="font-size: medium; font-family: 'B Nazanin'; font-weight: bold; position: relative; right: 170px" >
            &nbsp; ????? ??????? ????</td>
    </tr>

    <tr>
        <td class="style3">
            &nbsp;&nbsp;&nbsp;
            <asp:Label ID="Label1" runat="server" Text="  ???: " Font-Bold="True" 
                Font-Names="B Nazanin" Font-Size="Medium"></asp:Label>
        </td>
        <td class="style2">
            <asp:TextBox ID="FirstName" runat="server"></asp:TextBox>
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" Display="Dynamic" 
                ErrorMessage="???? ???? ??? ?????? ???" ControlToValidate="FirstName">*</asp:RequiredFieldValidator>
        </td>
    </tr>

    <tr>
        <td class="style3">
            &nbsp;&nbsp;&nbsp;<asp:Label ID="Label2" runat="server" Text=" ??? ????????: " 
                Font-Bold="True" Font-Names="B Nazanin" Font-Size="Medium"></asp:Label>
        </td>
        <td class="style2">
            <asp:TextBox ID="LastName" runat="server"></asp:TextBox>
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator2" runat="server" Display="Dynamic" 
                ErrorMessage="???? ???? ??? ???????? ?????? ???" 
                ControlToValidate="LastName">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style3">
            &nbsp;&nbsp;&nbsp;<asp:Label ID="Label3" runat="server" Text=" ????? ???????? : " 
              Font-Bold="True" Font-Names="B Nazanin" Font-Size="Medium"></asp:Label>
        </td>
        <td class="style2">
            <asp:TextBox ID="StudentNumber" runat="server"></asp:TextBox>
        </td>
        <td class="style4">
            <asp:RequiredFieldValidator ID="RequiredFieldValidator3" runat="server" Display="Dynamic" 
                ErrorMessage="???? ???? ????? ???????? ?????? ???" 
                ControlToValidate="StudentNumber">*</asp:RequiredFieldValidator>
        </td>
    </tr>
    <tr>
        <td class="style3">
            &nbsp;&nbsp;<asp:Label ID="Label4" runat="server" Text="  ????? ???? : "
              Font-Bold="True" Font-Names="B Nazanin" Font-Size="Medium"></asp:Label>
        </td>
        <td class="style2">
            <asp:TextBox ID="DateOfBirth" runat="server"></asp:TextBox>
        </td>
        <td class="style4">
            <asp:CompareValidator ID="CompareValidator1" runat="server" Display="Dynamic" 
                ErrorMessage="????? ???? ?????? ?? ???? ??????" Operator="DataTypeCheck" 
                Type="Date" ControlToValidate="dateOfBirth"></asp:CompareValidator>
        </td>
    </tr>
    <tr>
        <td class="style3">
            &nbsp;</td>
        <td class="style2">
            <asp:Button ID="SaveButton" runat="server" Text=" ????? ???????" Width="102px" 
                style="margin-right: 15px; height: 26px;"  />
        </td>
        <td class="style4">
            <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
                ConnectionString="<%$ ConnectionStrings:ASPNETDBConnectionString1 %>" 
                SelectCommand="SELECT aspnet_personalInformation.FirstName, aspnet_personalInformation.LastName, aspnet_personalInformation.StudentNumber, aspnet_personalInformation.DateOfBirth
                 FROM aspnet_personalInformation 
                 INNER JOIN aspnet_Users ON aspnet_personalInformation.UserId = aspnet_Users.UserId
                  WHERE    aspnet_personalInformation.UserId=aspnet_Users.UserId
                 ORDER BY aspnet_personalInformation.LastName"
                 InsertCommand="INSERT INTO aspnet_PersonalInformation(UserId) SELECT UserId FROM aspnet_Profile">
            </asp:SqlDataSource>
        </td>
    </tr>
</table>        

i wanna save firstname lastname studentnumber and dateofbirth in aspnet_personalinformation table in database but before that, i fill one column of aspnet_personalinformation table named UserId by inserting sql command with aspnet_profile.userid now by running this code my table has still blankes

  protected void SaveButton_Click(object sender, EventArgs e)
{
    string str= "Data Source=.\\SQLEXPRESS;AttachDbFilename=|DataDirectory|\\ASPNETDB.MDF;Integrated Security=True;User Instance=True";
    SqlConnection con = new SqlConnection(str);
    con.Open();
    string query = "INSERT INTO aspnet_PersonalInformation( FirstName, LastName,StudentNumber,DateOfBirth) VALUES ('" + this.FirstName.Text + "','" + this.LastName.Text + "','" + this.StudentNumber.Text + "','" + this.DateOfBirth.Text + "')    where aspnet_PersonalInformation.UserId=aspnet_Profile.UserID";
     SqlCommand cmd=new SqlCommand(query,con); 
    cmd.ExecuteNonQuery(); 
    con.Close();

}

but it doesn't work help me plz

© Stack Overflow or respective owner

Related posts about c#

Related posts about ASP.NET