Click here to Skip to main content
15,910,358 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
XML
<table>
        <tr>
            <td><asp:Label ID="Labelcontact1" runat="server" Text="Contact 1" CssClass="label"></asp:Label></td>
            <td><asp:TextBox ID="TextBoxContact1" runat="server" CssClass="textbox" ValidationGroup="updatebasics"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Label ID="Labelcontact2" runat="server" Text="Contact 2" CssClass="label"></asp:Label></td>
            <td><asp:TextBox ID="TextBoxContact2" runat="server" CssClass="textbox" ValidationGroup="updatebasics"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Label ID="Labelemail1" runat="server" Text="Email 1" CssClass="label"></asp:Label></td>
            <td><asp:TextBox ID="TextBoxEmail1" runat="server" CssClass="textbox" ValidationGroup="updatebasics"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Label ID="Labelemail2" runat="server" Text="Email 2" CssClass="label"></asp:Label></td>
            <td><asp:TextBox ID="TextBoxEmail2" runat="server" CssClass="textbox" ValidationGroup="updatebasics"></asp:TextBox></td>
        </tr>
        <tr>
            <td><asp:Button ID="ButtonSubmit" runat="server" Text="Update" CssClass="button" OnClick="ButtonSubmit_Click" ValidationGroup="updatebasics"/></td>
        </tr>
    </table>




C#
public partial class Default2 : System.Web.UI.Page
{
    static string connstr = ConfigurationManager.ConnectionStrings["ethena"].ConnectionString;
    private static SqlConnection con = new SqlConnection(connstr);
    private static SqlCommand cmd;
    protected void Page_Load(object sender, EventArgs e)
    {
        cmd = new SqlCommand("select * from page_content", con);
        con.Open();
        SqlDataReader reader = cmd.ExecuteReader();
        while (reader.Read())
        {
            TextBoxContact1.Text=reader["contact1"].ToString();
            TextBoxContact2.Text = reader["contact2"].ToString();
            TextBoxEmail1.Text = reader["email1"].ToString();
            TextBoxEmail2.Text = reader["email2"].ToString();
        }
        con.Close();
    }
    protected void ButtonSubmit_Click(object sender, EventArgs e)
    {
        string query="UPDATE page_content SET contact1='"+TextBoxContact1.Text.Trim()+"',contact2='"+TextBoxContact2.Text.Trim()+"',email1='"+TextBoxEmail1.Text.Trim()+"',email2='"+TextBoxEmail2.Text.Trim()+"'";
        //Labelcontact1.Text = query;
        cmd = new SqlCommand(query, con);
        con.Open();
        cmd.ExecuteReader();
        con.Close();
    }
}




query is not working on button click but when i run this query in sql it works nor give any error
Posted
Updated 25-Aug-13 10:12am
v2

Use
cmd.ExecuteNonQuery();

instead of
cmd.ExecuteReader();

I would like to suggest you to read more about ADO.Net
 
Share this answer
 
v2
Comments
Adarsh chauhan 26-Aug-13 2:21am    
I agree with Saqib..
Also I would suggest you to use parameters in your query instead of string concatenation to avoid sql injection.
i need to pack my page load event's code within if(!ispostback)
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

  Print Answers RSS
Top Experts
Last 24hrsThis month


CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900