Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9216699
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T02:28:22+00:00 2026-06-18T02:28:22+00:00

I am trying to develop a sample registration page using ASP.Net and C#. I

  • 0

I am trying to develop a sample registration page using ASP.Net and C#. I am calling a stored procedure to insert the data to database. My database is SQL Server 2008.

This is my code:

public partial class Sample : System.Web.UI.Page
{
   SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["dbconnection"].ToString());
   string str;

protected void Page_Load(object sender, EventArgs e)
{
    rbt_Male.Checked = true;
}

protected void btn_Submit_Click(object sender, EventArgs e)
{
    string @Name = txtbx_Name.Text;
    string @Gender_male = rbt_Male.Text;
    string @Gender_Female = rbt_Female.Text;
    string @Email = txtbx_Email.Text;
    DateTime @Dob = Convert.ToDateTime(txt_Dob.Text);
    submitdata();
}

protected void submitdata()
{
    try
    {
        SqlCommand cmd = new SqlCommand();
        cmd.Parameters.Clear();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "insertdata";

        if (rbt_Male.Checked)
        {
            cmd.Parameters.AddWithValue("@Name", txtbx_Name.Text);
            cmd.Parameters.AddWithValue("@Gender_Male", rbt_Male.Text);
            cmd.Parameters.AddWithValue("@Email", txtbx_Email.Text);
            cmd.Parameters.AddWithValue("@Dob", Convert.ToDateTime(txt_Dob.Text));
        }
        else if (rbt_Female.Checked)
        {
            cmd.Parameters.AddWithValue("@Name", txtbx_Name.Text);
            cmd.Parameters.AddWithValue("@Gender_Female", rbt_Male.Text);
            cmd.Parameters.AddWithValue("@Email", txtbx_Email.Text);
            cmd.Parameters.AddWithValue("@Dob", Convert.ToDateTime(txt_Dob.Text));
        }

        if (con.State == ConnectionState.Closed)
            con.Open();

        cmd.ExecuteNonQuery();

        lbl_Errormsg.Visible = true;
        lbl_Errormsg.Text = "Record Inserted Successfully";
        con.Close();
    }
    catch (Exception ex)
    {
        lbl_Errormsg.Visible = true;
        lbl_Errormsg.Text = ex.Message;
    }

I am getting the error message

ExecuteNonQuery: Connection property has not been initialized.

I am getting this error at cmd.ExecuteNonQuery();

Please help me.

My stored procedure is

ALTER Procedure insertdata
(
 @Name Varchar(20),
 @Gender Varchar(6),
 @Email Varchar(20),
 @Dob date
) 
As
 Begin
   Insert into samplelogintable (Name, Gender, Email, Dob)
   Values(@Name, @Gender, @Email, @Dob)
 End
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-18T02:28:23+00:00Added an answer on June 18, 2026 at 2:28 am

    You haven’t associated your command cmd with your SqlConnection, that is why you are getting the error.

    You need to specify:

    cmd.Connection = con;
    

    in your submitdata() method.

    Since SqlCommand implements IDisposable, its better if you use it within using block like:

    using (SqlCommand cmd = new SqlCommand())
    {
    
        cmd.Parameters.Clear();
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.CommandText = "insertdata";
        cmd.Connection = con;
        .... your code
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm trying to develop a custom report for trac using SQL. On one of
I am trying to develop ASP.NET web application and also Android UI for the
I am trying to develop a simple sample mobile web app using MVC4, to
I am using Oracle ODAC for EntityFramework. I am trying to develop a sample
I'm trying to develop a simple kernel using TASM, using this code: ; beroset.asm
I'm trying to develop a simple php based schedule ... This is my database
I am trying to develop a sample application that finds the process name of
I'm trying to develop an Audio Capture application using the AudioRecord class from android
I am trying to develop an sample project in Django and getting errors when
I'm trying develop a sample application with multi language files. I use: Windows 7

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.