Click here to Skip to main content
15,885,757 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

public partial class Register : System.Web.UI.Page
{
    DBConnection dbcon = new DBConnection();
    String constr= System.Configuration.ConfigurationManager.AppSettings["constr"].ToString();
    SqlConnection con;
    Int16 ucode;

    
    protected void btnregsubmit_Click(object sender, ImageClickEventArgs e)
    {
        SqlCommand cmd = new SqlCommand();
        try
        {
            con = new SqlConnection(constr);
            cmd = new SqlCommand("insertuser", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@usertype", ucode);
            cmd.Parameters.AddWithValue("@sqrtyans", txtans.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("<script>alert('Data is inserted')</script>");
        }
        catch (Exception ex)
        {
            throw ex;
        }
    }
}
Posted
Updated 2-Oct-13 1:08am
v2

1 solution

i have modify your try block just copy and paste in your code

C#
try
        {
            con = new SqlConnection(constr);
            con.Open();
            cmd = new SqlCommand("insertuser", con);
            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.AddWithValue("@usertype", ucode);
            cmd.Parameters.AddWithValue("@sqrtyans", txtans.Text);
            cmd.ExecuteNonQuery();
            con.Close();
            Response.Redirect("<script>alert('Data is inserted')</script>");
        }



Your Have not open the connection to execute your sql query
 
Share this answer
 
Comments
pkvartej7121 2-Oct-13 7:56am    
Thank you very much Salmanbhai.

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



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