473,394 Members | 1,845 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,394 software developers and data experts.

C# ASP.NET SQL Command running stored procedure

Hello,

Thanks in advance for any insight you can offer. I've a ASP.NET project
written in C#, two web forms, a lovely gob of using statements. I originally
had one webform with all my fields and datagrid stacked, and thought it would
be cleaner to server.transfer to another webform when I wanted to submit a
new record. When I had just one webform this same syntax worked fine. But now
that I'm not on the same webform as where the datagrid resides, I had to
change the code to use a sqlCommand. I am not presently getting an error, and
it builds fine. (By the way, I did turn off the custom error messages but it
doesn't give me the full error page.)

Earlier on I was getting errors like 'input string not in correct format',
but those seemed to have cleared. I also got an error that was a null object
reference and I seem to have resolved that too.

again thanks,

Samantha

try

{
SqlConnection sqlCon6 = new SqlConnection(
"Server=DP05900;database=InfoSvcs_ServerLogs;uid=s a;password=cr3ati0n;");
SqlCommand sqlAddLog = new SqlCommand ("sqlAddLog", sqlCon6);
sqlCon6.Open();
sqlAddLog.CommandType = CommandType.StoredProcedure;
sqlAddLog.CommandText = ("exec sqlAddLog");

sqlAddLog.Parameters.Add("@Assigned_To",System.Dat a.SqlDbType.VarChar,20).Value = ddlAdmins.SelectedItem.Text; ;
sqlAddLog.Parameters.Add("@Entry_Date_Time",System .Data.SqlDbType.DateTime, 8).Value = TxtDate.Text;
sqlAddLog.Parameters.Add("@Comments",System.Data.S qlDbType.VarChar,5000).Value = TxtAction.Text;
sqlAddLog.Parameters.Add("@Server_Id",System.Data. SqlDbType.Int).Value =
ddlServers.SelectedValue;
Console.WriteLine();
sqlCon6.Close();

// Server.Transfer ("ControlPanel.aspx");

}

catch ( System.Exception Ex )
{
Console.WriteLine();

LblError.Text =
Ex.Message;
}

finally
{
if (sqlCon6 != null)
sqlCon6.Close();
}
}
Nov 17 '05 #1
7 2529
You don't mention what the problem is but just glancing at it I see a
few things wrong.

1.) No call to ExecuteNonQuery() on the command object so no call to
the database to update/insert will ever be made.
2.) You try to reference your SqlCon6 object in the finally block but
it is defined in the try block so won't be visible to the finally
block. Define it above the try block as SqlConnection sqlCon6 = null;
3.) You have a call to sqlCon6.Close() in both your try block and your
finally block, it is only needed in your finally block.

Nov 17 '05 #2
Oops, I missed the line
sqlAddLog.CommandText = ("exec sqlAddLog");
Get rid of it, your define the command text correctly in the first
parameter of the constructor.

Nov 17 '05 #3
Thanks.

1. I commented out the sqlAddLog.CommandText = ("exec sqlAddLog");
2. I couldn't add the sqlCon6 = null above the try since it wouldn't build
.... saying it would cause sqlCon6 to mean something other than what it
currently meant.
3. I commented out the sqlCon6.Close(); that is found in my try statement.
4. Now I am getting the original error, which I'd thought I'd eliminated ...
Object reference not set to an instance of an object.

I've read a great deal of Google and MSDN posts on this. I know its a
"newbie error" but for the life of me, I can't see what I've not instantiated.

Thanks again.

"sd********@gmail.com" wrote:
Oops, I missed the line
sqlAddLog.CommandText = ("exec sqlAddLog");
Get rid of it, your define the command text correctly in the first
parameter of the constructor.

Nov 17 '05 #4
Also, I didn't specify that the code, written as it was originally, didn't
generate an error nor did it create a row in the database. The stored
procedure works fine both through Query Analyzer and also when referenced in
my original code.

Thanks,

S

"sd********@gmail.com" wrote:
Oops, I missed the line
sqlAddLog.CommandText = ("exec sqlAddLog");
Get rid of it, your define the command text correctly in the first
parameter of the constructor.

Nov 17 '05 #5
It should look similar to this (I can't try compiling myself right now,
not on a dev machine):

SqlConnection sqlCon6 = null;
try
{
sqlCon6 = new
SqlConnection("Server=DP05900;database=InfoSvcs_Se rverLogs;uid=sa;password=cr3ati0n;");
SqlCommand sqlAddLog = new SqlCommand ("sqlAddLog",
sqlCon6);
sqlCon6.Open();
sqlAddLog.CommandType = CommandType.StoredProcedure;

sqlAddLog.Parameters.Add("@Assigned_To",System.Dat a.SqlDbType.VarChar,20).Value
= ddlAdmins.SelectedItem.Text; ;

sqlAddLog.Parameters.Add("@Entry_Date_Time",System .Data.SqlDbType.DateTime,
8).Value = TxtDate.Text;

sqlAddLog.Parameters.Add("@Comments",System.Data.S qlDbType.VarChar,5000).Value
= TxtAction.Text;

sqlAddLog.Parameters.Add("@Server_Id",System.Data. SqlDbType.Int).Value
=
ddlServers.SelectedValue;

sqlAddLog.ExecuteNonQuery();

}

catch ( System.Exception Ex )
{

LblError.Text =
Ex.Message;
}

finally
{
if (sqlCon6 != null)
sqlCon6.Close();
}
}
That exception you're seeing is pretty ambiguous, which line exactly is
causing it (turn on "break into debugger" for all exceptions by hitting
alt-ctr-e, highlighting clr exceptions, then selecting break into
debugger from the radio buttons).

Nov 17 '05 #6
Hi,

remove this line:
sqlAddLog.CommandText = ("exec sqlAddLog");

You do not call any executing method once you have your parameters, you have
to call one of:
ExecuteReader
ExecuteScalar
ExecuteNonQuery

depending of what you want to return.
after that you should check your DB and see if the row was inserted.
cheers,

--
Ignacio Machin,
ignacio.machin AT dot.state.fl.us
Florida Department Of Transportation

"Samantha Penhale" <Sa*************@discussions.microsoft.com> wrote in
message news:B9**********************************@microsof t.com...
Hello,

Thanks in advance for any insight you can offer. I've a ASP.NET project
written in C#, two web forms, a lovely gob of using statements. I
originally
had one webform with all my fields and datagrid stacked, and thought it
would
be cleaner to server.transfer to another webform when I wanted to submit a
new record. When I had just one webform this same syntax worked fine. But
now
that I'm not on the same webform as where the datagrid resides, I had to
change the code to use a sqlCommand. I am not presently getting an error,
and
it builds fine. (By the way, I did turn off the custom error messages but
it
doesn't give me the full error page.)

Earlier on I was getting errors like 'input string not in correct format',
but those seemed to have cleared. I also got an error that was a null
object
reference and I seem to have resolved that too.

again thanks,

Samantha

try

{
SqlConnection sqlCon6 = new SqlConnection(
"Server=DP05900;database=InfoSvcs_ServerLogs;uid=s a;password=cr3ati0n;");
SqlCommand sqlAddLog = new SqlCommand ("sqlAddLog", sqlCon6);
sqlCon6.Open();
sqlAddLog.CommandType = CommandType.StoredProcedure;
sqlAddLog.CommandText = ("exec sqlAddLog");

sqlAddLog.Parameters.Add("@Assigned_To",System.Dat a.SqlDbType.VarChar,20).Value
= ddlAdmins.SelectedItem.Text; ;
sqlAddLog.Parameters.Add("@Entry_Date_Time",System .Data.SqlDbType.DateTime,
8).Value = TxtDate.Text;
sqlAddLog.Parameters.Add("@Comments",System.Data.S qlDbType.VarChar,5000).Value
= TxtAction.Text;
sqlAddLog.Parameters.Add("@Server_Id",System.Data. SqlDbType.Int).Value =
ddlServers.SelectedValue;
Console.WriteLine();
sqlCon6.Close();

// Server.Transfer ("ControlPanel.aspx");

}

catch ( System.Exception Ex )
{
Console.WriteLine();

LblError.Text =
Ex.Message;
}

finally
{
if (sqlCon6 != null)
sqlCon6.Close();
}
}

Nov 17 '05 #7
Perfecto! Thank you so much! Now I understand both of the major changes...

sqlConnection sqlCon6 = null;

sqlAddLog.ExecuteNonQuery();

The syntax you gave me worked perfectly.

Samantha

Nov 17 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: Bruno Alexandre | last post by:
Hi guys, withou using SP, I want to be able to add a Parameter to the SQL Query and retrive the Recordset so I can use the Paging property under the recorset object.... how can I do this? I'm...
4
by: Tom | last post by:
I want to open a recordset object on an .asp page. When I open the recordset I would like to use a stored procedure that expects a parameter to be passed for the stored procedure. I will then use...
1
by: Sandie Towers | last post by:
We use a number of similar databases and frequently create a new database using a backup restore of another similar database. We try to keep changes between databases in _Additional tables - like...
1
by: Rittercorp | last post by:
I am debugging an app which blocks many processes in a SQL7 server DB. The app log writes every transaction "open" and "close". The weird thing is : when the app logfile says the transaction is...
8
by: Ivan | last post by:
Hi I am new in DB2, and I have some problems when I try run stored procedures, and others statements. I made one stored procedure very simple, but this show different messages. I have followed...
4
by: nishi57 | last post by:
I hope I can get some help regarding this issue, which has been going on for a while. I have a desktop user who is having problem running "Stored Procedures". The DB2 Connect application works fine...
8
by: Ben | last post by:
Hi! I already sent this to the ACCESS newsgroup. But since I do not know really which side is really causing the problem, I have decided to send this inquiry to this newsgroup also, if I may....
2
by: db2learner | last post by:
Hi, I am new to DB2 and i just started worked on it a couple of days back. I have created basic EMPLOYEE table from control centre which has 2 fields: EmpNo, EmpName. I am trying to write...
1
by: David Greenberg | last post by:
Hi We're using SqlServer 2000. I want to run a DTS using the DTSRUN command. The commmand is inside a ".bat" file which my application is running from an SQL client. No matter how I run it...
4
by: enggwaqas | last post by:
I have an Oracle stored procedure that takes approx 3 minutes to execute and I am using .net 2.0 data access provider for oracle. Now i want to cancel the execution of stored procedure from .net...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
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
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

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.