Click here to Skip to main content
12,077,911 members (47,432 online)
Rate this:
 
Please Sign up or sign in to vote.
See more: C# C#4.0
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:
"ExecuteNonQuery: CommandText property has not been initialized".

plz help me asap.

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 20-Dec-12 1:14am
Edited 20-Dec-12 1:17am
v2
Comments
Rajesh Anuhya 20-Dec-12 6:18am
   
Edited: Code tags added.
--RA
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 1

write
cmd.CommandText = "sampleproc";
  Permalink  
v2
Rate this: bad
 
good
Please Sign up or sign in to vote.

Solution 2

Your Error is so clear.., You have missed the CommandText Property in your SqlCommand.
Check below link
Here[^]

Thanks
-RA
  Permalink  
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)

  Print Answers RSS
Top Experts
Last 24hrsThis month


Advertise | Privacy | Mobile
Web03 | 2.8.160212.1 | Last Updated 20 Dec 2012
Copyright © CodeProject, 1999-2016
All Rights Reserved. Terms of Service
Layout: fixed | fluid

CodeProject, 503-250 Ferrand Drive Toronto Ontario, M3C 3G8 Canada +1 416-849-8900 x 100