Click here to Skip to main content
15,886,665 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I have a stored procedure named "sampleproc" that accepts one parameter @rollno.
I am trying to run the below code but it is giving the error:
<br />
"ExecuteNonQuery: CommandText property has not been initialized".


plz help me asap.

C#
namespace WindowsFormsApplication1
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            string conn = "Data Source=.;Initial Catalog=LearningSP;Integrated Security=True";
            SqlConnection con = new SqlConnection(conn);
            con.Open();
           
            SqlCommand cmd = new SqlCommand();
            
           

            cmd.CommandType = CommandType.StoredProcedure;
            cmd.Parameters.Add(new SqlParameter("@rollno",100));
            cmd.Connection = con;
            cmd.ExecuteNonQuery();
            con.Close();
            
        }
    }
}
Posted
Updated 20-Dec-12 0:17am
v2
Comments
Rajesh Anuhya 20-Dec-12 6:18am    
Edited: Code tags added.
--RA

write
C#
cmd.CommandText = "sampleproc";
 
Share this answer
 
v2
Your Error is so clear.., You have missed the CommandText Property in your SqlCommand.
Check below link
Here[^]

Thanks
-RA
 
Share this answer
 
Comments
Member 9581909 20-Dec-12 6:29am    
thank you so much
Rajesh Anuhya 20-Dec-12 6:48am    
Welcome!!
-RA
Member 9581909 21-Dec-12 1:38am    
In the following code...i am not being able to set the commandtype as storedprocedure.i have commented out that line.
Its the back end coding of an asp.net 3.5 page with c#.



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;

public partial class NEWRECORD : System.Web.UI.Page
{


protected void Page_Load(object sender, EventArgs e)
{



}
protected void Button1_Click(object sender, EventArgs e)
{
string conn = "Data Source=.;Initial Catalog=School;Integrated Security=True";
SqlConnection con=new SqlConnection(conn);
con.Open();
SqlCommand cmd=new SqlCommand();
// cmd.CommandType=
cmd.Connection = con;

cmd.CommandText = "NEWREC";
int roll;
roll=Convert.ToInt16(TextBox1.Text);
int klass;
klass=Convert.ToInt16(TextBox3.Text);

cmd.Parameters.Add(new SqlParameter("@id",1));
cmd.Parameters.Add(new SqlParameter("@name",TextBox2.Text));
cmd.Parameters.Add(new SqlParameter("@class", klass));
cmd.ExecuteNonQuery();
con.Close();
}
}

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