2 Replies - 900 Views - Last Post: 04 December 2012 - 06:50 AM Rate Topic: -----

#1 saikiran.salla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 12

Button is not working in asp.net 4.0

Posted 04 December 2012 - 05:48 AM

I'm working on project in asp.net 4.0, at home. i created a web page, and i wrote a code in button, but when i debug or without debug i'm not enabling button command/function. here is the code.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class Home : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDB2per1"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "insert into registerUser values('" + txtFname.Text + "','" + txtLname.Text + "','" + txtLname.Text + "','" + txtDOB.Text + "','" + txtMbl.Text + "','" + txtPass.Text + "','" + txtEmail.Text + "','" + txtLname.Text + "','" + ddlState.SelectedItem.Text + "')";
        cmd.ExecuteNonQuery();
        con.Close();
        Session["usersession"] = txtMbl.Text;
        Response.Redirect("about.aspx");
        
    }
}


for clarity here is the attachment of my code. check it.

Is This A Good Question/Topic? 0
  • +

Replies To: Button is not working in asp.net 4.0

#2 DarenR  Icon User is offline

  • D.I.C Lover

Reputation: 485
  • View blog
  • Posts: 3,250
  • Joined: 12-January 10

Re: Button is not working in asp.net 4.0

Posted 04 December 2012 - 06:23 AM

why are you opening your connection out of the button?


public partial class Home : System.Web.UI.Page  

13 {  

14     SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDB2per1"].ConnectionString);  

15     SqlCommand cmd = new SqlCommand();  




you should put that in the button
Was This Post Helpful? 0
  • +
  • -

#3 saikiran.salla  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 02-December 12

Re: Button is not working in asp.net 4.0

Posted 04 December 2012 - 06:50 AM

Even i open my connection inside of button also i'm not getting any responce from button click. and i forgot to show my master page.
master page:
<%@ Master Language="C#" AutoEventWireup="true" CodeFile="home.master.cs" Inherits="_default" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <link href="../style.css" rel="stylesheet" type="text/css" />
    <title></title>
    <asp:ContentPlaceHolder id="head" runat="server">
    </asp:ContentPlaceHolder>
</head>
<body>
    <form id="form1" runat="server">
   
<div id="bg1">
	<div id="header">
		<h1><a href="#">2per1<sup>1.0</sup></a></h1>
		<h2>Free Mobile recharge</h2>
	</div>
	<!-- end #header -->
</div>
<!-- end #bg1 -->
<div id="bg2">
	<div id="header2">
		<div id="menu">
			<ul>
				<li><a href="UserHome.aspx">Home</a></li>
                <li><a href="UserGet.aspx">Get</a></li>
                <li><a href="UserRecharge.aspx">Recharge</a></li>
                <li><a href="UserProfile.aspx">Profile</a></li>
                <li><a href="UserHow.aspx">How it works</a></li>
				<li><a href="About.aspx">About</a></li>
				<li><a href="#">Contact</a></li>
			</ul>
		</div>
		<!-- end #menu -->
		<div id="search">
			<form method="get" action="#">
				<fieldset>
				<input type="text" name="q" value="search keywords" id="q" class="text" />
				<input type="submit" value="Search" class="button" />
				</fieldset>
			</form>
		</div>
		<!-- end #search -->
	</div>
	<!-- end #header2 -->
</div>
<!-- end #bg2 -->
<div id="bg3">
	<div id="bg4">
		<div id="bg5">
			<asp:ContentPlaceHolder id="ContentPlaceHolder1" runat="server">
        
        </asp:ContentPlaceHolder>
			<!-- end #page -->
		</div>
	</div>
</div>
<!-- end #bg3 -->
<div id="footer">
	<p>(c) 2012 2per1.com Design by <a href="http://www.www.com/">SsaiK</a> + All Rights Reserved</p>
</div>

        
    </form>
</body>
</html>



and .aspx page

<%@ Page Title="" Language="C#" MasterPageFile="~/default.master" AutoEventWireup="true" CodeFile="Home.aspx.cs" Inherits="Home" %>

<asp:Content ID="Content1" ContentPlaceHolderID="head" Runat="Server">
</asp:Content>
<asp:Content ID="Content2" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
    <div id="page">
<table>
<tr>
<td width="310px">
<table>
<tr>
<td>
First Name:</td>
<td>
    <asp:TextBox ID="txtFname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Last Name:</td>
<td>
    <asp:TextBox ID="txtLname" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Date of Birth:</td>
<td>
    <asp:TextBox ID="txtDOB" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    Gender:</td>
<td>
    <asp:RadioButton ID="rBtnMale" runat="server" GroupName="gender" Text="Male" />
    <asp:RadioButton ID="rBtnFemale" runat="server" GroupName="gender" 
        Text="Female" />
</td>
</tr>
<tr>
<td>
Mobile Number:</td>
<td>
    <asp:TextBox ID="txtMbl" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Occupation:</td>
<td>
    <asp:TextBox ID="txtOcc" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
email:
</td>
<td>
    <asp:TextBox ID="txtEmail" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
password:
</td>
<td>
    <asp:TextBox ID="txtPass" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Country:</td>
<td>
    <asp:TextBox ID="txtCountry" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
State:</td>
<td>
    <asp:DropDownList ID="ddlState" runat="server">
        <asp:ListItem>Andhra Pradesh</asp:ListItem>
        <asp:ListItem>Tamilnadu</asp:ListItem>
        <asp:ListItem>Kerala</asp:ListItem>
    </asp:DropDownList>
</td>
</tr>
<tr>
<td>
Capcha:</td>
<td>
    <asp:TextBox ID="txtCaptha" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td colspan="2">
    <asp:CheckBox ID="ckbAccept" runat="server" />Accept conditions
    <asp:Label ID="Label1" runat="server" Text="Label"></asp:Label>
    <asp:SqlDataSource ID="SqlDataSource1" runat="server" 
        ConnectionString="<%$ ConnectionStrings:ASPNETDB2per1 %>" 
        DeleteCommand="DELETE FROM [registerUser] WHERE [mnum] = @mnum" 
        InsertCommand="INSERT INTO [registerUser] ([fname], [lname], [gender], [dof], [mnum], [password], [email], [country], [state]) VALUES (@fname, @lname, @gender, @dof, @mnum, @password, @email, @country, @state)" 
        ProviderName="<%$ ConnectionStrings:ASPNETDB2per1.ProviderName %>" 
        SelectCommand="SELECT [sno], [fname], [lname], [gender], [dof], [mnum], [password], [email], [country], [state] FROM [registerUser]" 
        UpdateCommand="UPDATE [registerUser] SET [sno] = @sno, [fname] = @fname, [lname] = @lname, [gender] = @gender, [dof] = @dof, [password] = @password, [email] = @email, [country] = @country, [state] = @state WHERE [mnum] = @mnum">
        <DeleteParameters>
            <asp:Parameter Name="mnum" Type="String" />
        </DeleteParameters>
        <InsertParameters>
            <asp:Parameter Name="fname" Type="String" />
            <asp:Parameter Name="lname" Type="String" />
            <asp:Parameter Name="gender" Type="String" />
            <asp:Parameter Name="dof" Type="String" />
            <asp:Parameter Name="mnum" Type="String" />
            <asp:Parameter Name="password" Type="String" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="country" Type="String" />
            <asp:Parameter Name="state" Type="String" />
        </InsertParameters>
        <UpdateParameters>
            <asp:Parameter Name="sno" Type="Int32" />
            <asp:Parameter Name="fname" Type="String" />
            <asp:Parameter Name="lname" Type="String" />
            <asp:Parameter Name="gender" Type="String" />
            <asp:Parameter Name="dof" Type="String" />
            <asp:Parameter Name="password" Type="String" />
            <asp:Parameter Name="email" Type="String" />
            <asp:Parameter Name="country" Type="String" />
            <asp:Parameter Name="state" Type="String" />
            <asp:Parameter Name="mnum" Type="String" />
        </UpdateParameters>
    </asp:SqlDataSource>
</td>
</tr>
<tr>
<td>
</td>
<td>
    <asp:Button ID="btnReg" runat="server" Text="Register" onclick="btnReg_Click" />
    &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;
    <asp:Button ID="btnReset" runat="server" Text="Reset" />
</td>
</tr>
</table>
</td>
<td width="280px">
<table>
<tr>
<td>

</td>
</tr>
</table>
</td>
<td align="right" width="310px">
<table>
<tr>
<td>
UserName:</td>
<td>
<asp:TextBox ID="TextBox11" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
Password:</td>
<td><asp:TextBox ID="TextBox12" runat="server"></asp:TextBox>
</td>
</tr>
<tr>
<td>
    
</td>
<td>
    <asp:Button ID="Button1" runat="server" Text="Login" />&nbsp; <asp:HyperLink ID="HyperLink1" runat="server">Forgot password?</asp:HyperLink>
</td>
</tr>
</table>
</td>
</tr>
</table>
</div>

</asp:Content>



and home.aspx.cs
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


public partial class Home : System.Web.UI.Page
{
    SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ASPNETDB2per1"].ConnectionString);
    SqlCommand cmd = new SqlCommand();
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
    protected void btnReg_Click(object sender, EventArgs e)
    {
        con.Open();
        cmd.Connection = con;
        cmd.CommandText = "insert into register values('" + txtFname.Text + "','" + txtLname.Text + "','" + txtLname.Text + "','" + txtDOB.Text + "','" + txtMbl.Text + "','" + txtPass.Text + "','" + txtEmail.Text + "','" + txtLname.Text + "','" + ddlState.SelectedItem.Text + "')";
        cmd.ExecuteNonQuery();
        con.Close();
        Session["usersession"] = txtMbl.Text;
        Response.Redirect("about.aspx");
        
    }
}


Was This Post Helpful? 0
  • +
  • -

Page 1 of 1