473,421 Members | 1,461 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,421 software developers and data experts.

Stored Procedure using SQLDataAdapter , DropDownList

39
Hi

I have to bind the DropDownList box with ID field hidden using SQLDataAdapter.

For that I have given the coding as

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{

SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER;");
con.Open();
SqlCommand cmd = new SqlCommand();
cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Pro_Specialty";
cmd.Connection = con;

SqlParameter SPID = new SqlParameter("@SPID ", SqlDbType.NVarChar, 12);
SPID.Direction = ParameterDirection.Output;
Specialty_DropDownList.Text = SPID.Value.ToString();

SqlParameter Specialty = new SqlParameter("@Specialty ", SqlDbType.NVarChar, 50);
Specialty.Direction = ParameterDirection.Output;
Specialty_DropDownList.Text = Specialty.Value.ToString();
cmd.Parameters.Add(SPID);
cmd.Parameters.Add(Specialty);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Specialty_DropDownList.DataValueField = "SPID";

Specialty_DropDownList.DataTextField = "Specialty";

Specialty_DropDownList.DataSource = ds;

Specialty_DropDownList.DataBind();
con.Close();


}
}

I have created the stored procedure in sql server 2005 as

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go




-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE Pro_Specialty

@SPID nvarchar(12)output,
@Specialty nvarchar(50)output


AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.


-- Insert statements for procedure here
select @SPID=SPID,@Specialty=Specialty from Specialty

END

But if I run, I get the error

Object reference not set to an instance of the object.
Jun 28 '08 #1
2 3249
kenobewan
4,871 Expert 4TB
Your select statement is a likely culprit here, try:
Expand|Select|Wrap|Line Numbers
  1. insert into Specialty (SPID,Specialty)
  2. values(@SPID,@Specialty)
Why is the field name and table name the same? Means that you are still likely to get an error? You may also run into probs later inserting the id this way.

You may need further resources to learn about sql :hint:, try stickies, tutorials etc.
Jun 28 '08 #2
suganya
39
Your select statement is a likely culprit here, try:
Expand|Select|Wrap|Line Numbers
  1. insert into Specialty (SPID,Specialty)
  2. values(@SPID,@Specialty)
Why is the field name and table name the same? Means that you are still likely to get an error? You may also run into probs later inserting the id this way.

You may need further resources to learn about sql :hint:, try stickies, tutorials etc.
Hi Kenobewan

I have changed the select statement into the statement that U have given above as

set ANSI_NULLS ON
set QUOTED_IDENTIFIER ON
go





-- =============================================
-- Author: <Author,,Name>
-- Create date: <Create Date,,>
-- Description: <Description,,>
-- =============================================
ALTER PROCEDURE [dbo].[Pro_Specialty]

@SPID nvarchar(12)output,
@Specialty nvarchar(50)output


AS
BEGIN
-- SET NOCOUNT ON added to prevent extra result sets from
-- interfering with SELECT statements.


-- Insert statements for procedure here
-- select @SPID=SPID,@Specialty=Specialty from Specialty
insert into Specialty (SPID,Specialty)
values(@SPID,@Specialty)

END




In form I have given the coding as

protected void Page_Load(object sender, EventArgs e)
{

if (!Page.IsPostBack)
{

SqlConnection con = new SqlConnection("user id=sa;password=cast;database=Hello_Dr;server=AUROR A-SERVER;");
con.Open();
SqlCommand cmd = new SqlCommand();

cmd.CommandType = CommandType.StoredProcedure;
cmd.CommandText = "Pro_Specialty";
cmd.Connection = con;

SqlParameter SPID = new SqlParameter("@SPID ", SqlDbType.NVarChar, 12);
SPID.Direction = ParameterDirection.Output;
Specialty_DropDownList.Text = SPID.Value.ToString();

SqlParameter Specialty = new SqlParameter("@Specialty ", SqlDbType.NVarChar, 50);
Specialty.Direction = ParameterDirection.Output;
Specialty_DropDownList.Text = Specialty.Value.ToString();

cmd.Parameters.Add(SPID);
cmd.Parameters.Add(Specialty);
cmd.ExecuteNonQuery();


SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
Specialty_DropDownList.DataValueField = "@SPID";

Specialty_DropDownList.DataTextField = "@Specialty";

Specialty_DropDownList.DataSource = ds;

Specialty_DropDownList.DataBind();
con.Close();



}

But still I m getting the error as

Object reference not set to an instance of an object.
Jun 30 '08 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Mullin Yu | last post by:
i have a stored procedure at sql server 2k. which will update records and select result from temp table. if i use SqlConnection class, and i do both. but, if i use OleDbConnection class, i can...
2
by: Dino L. | last post by:
How can I run stored procedure (MSSQL) ?
0
by: Amber | last post by:
Stored procedures are faster and more efficient than in-line SQL statements. In this article we will look at two SQL Server stored procedures; one using an input parameter and one not, and see how...
2
by: Mike Hutton | last post by:
I have a rather odd problem. I have a SP which uses temp. tables along the way, and then returns a table of results: CREATE PROCEDURE dbo.usp_myproc( @pNameList VARCHAR(6000) ) AS
9
by: Nikolay Petrov | last post by:
How to fill DataSet from stored procedure?
2
by: Roy | last post by:
Hey all, Here's a small VB codeblock that connects to a database and uses 2 SQL queries then forms a relation for a master/detail view on the aspx side: Private Sub Binddata(ByVal name As...
5
by: ric_deez | last post by:
Hi there, I would like to create a simple search form to allow users to search for a job number based on a number of parameters. I think I understand how to use parameteres associated with Stored...
9
by: fniles | last post by:
I am using VB.NET 2003 and SQL2000 database. I have a stored procedure called "INSERT_INTO_MYTABLE" that accepts 1 parameter (varchar(10)) and returns the identity column value from that table....
1
by: sweatha | last post by:
Hi I am quite new in writing stored procedure. I have created a stored procedure for binding the data from DB. I have to bind data from Specialty table with 2 feilds. For that my stored procedure...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.