ASP.NET/C#->MS SQL help

V

Viktor Popov

Hi,
I have trouble with the following:
I have this Stored Procedure:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'prUPUSERINFO' AND type = 'P')
DROP PROCEDURE prUPUSERINFO
GO
CREATE PROCEDURE prUPUSERINFO
@USRNAME VARCHAR(20),
@NAME VARCHAR(64),
@CITY VARCHAR(20),
@PHONE VARCHAR(16),
@ADDR VARCHAR(74),
@EML VARCHAR(64)
AS
UPDATE BLEK.USERS
SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME

When I execute it everythings is fine.
I develop ASP.NET C# Application
and do the following in one of the forms:

private void InsertBtn_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter dada = new SqlDataAdapter("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
dada.UpdateCommand.CommandType = CommandType.Text;
conn.Open();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@USRNAME"].Value = Session["usrName"];
dada.UpdateCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@NAME"].Value = NameTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@CITY",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@CITY"].Value = CityTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@PHONE",
SqlDbType.VarChar,16));
dada.UpdateCommand.Parameters["@PHONE"].Value = PhoneTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@ADDR",
SqlDbType.VarChar,74));
dada.UpdateCommand.Parameters["@ADDR"].Value = AddressTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@EML",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@EML"].Value = EmailTB.Text.Trim();
dada.UpdateCommand.ExecuteNonQuery();
conn.Close();


}
Could you tell me waths the problem here?
When I Push the Button Insert The following error appears:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 104: SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
Line 105: SqlDataAdapter dada = new SqlDataAdapter("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
Line 106: dada.UpdateCommand.CommandType = CommandType.Text;
Line 107: conn.Open();
Line 108: dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));


Source File: c:\inetpub\wwwroot\estates\pdata.aspx.cs Line: 106

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Estates.pdata.InsertBtn_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\estates\pdata.aspx.cs:106
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()




Thank you very much!

Viktor
 
H

Hans Kesting

}
Could you tell me waths the problem here?
When I Push the Button Insert The following error appears:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 104: SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
Line 105: SqlDataAdapter dada = new SqlDataAdapter("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
Line 106: dada.UpdateCommand.CommandType = CommandType.Text;

Where did you specify this "UpdateCommand"? The sql text you specify (granted, it IS
an update statement) is used as "SelectCommand" ! Thus there is no UpdateCommand
and you get this error when you try to use it.

use this:
SqlDataAdapter dada = new SqlDataAdapter();
dada.UpdateCommand = new SqlCommand("Update .......", conn);

Hans Kesting
 
R

Rutger Smit

Viktor said:
Hi,
I have trouble with the following:
I have this Stored Procedure:
IF EXISTS (SELECT name FROM sysobjects
WHERE name = 'prUPUSERINFO' AND type = 'P')
DROP PROCEDURE prUPUSERINFO
GO
CREATE PROCEDURE prUPUSERINFO
@USRNAME VARCHAR(20),
@NAME VARCHAR(64),
@CITY VARCHAR(20),
@PHONE VARCHAR(16),
@ADDR VARCHAR(74),
@EML VARCHAR(64)
AS
UPDATE BLEK.USERS
SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME

When I execute it everythings is fine.
I develop ASP.NET C# Application
and do the following in one of the forms:

private void InsertBtn_Click(object sender, System.EventArgs e)
{
SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
SqlDataAdapter dada = new SqlDataAdapter("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
dada.UpdateCommand.CommandType = CommandType.Text;
conn.Open();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@USRNAME"].Value = Session["usrName"];
dada.UpdateCommand.Parameters.Add(new SqlParameter("@NAME",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@NAME"].Value = NameTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@CITY",
SqlDbType.VarChar,20));
dada.UpdateCommand.Parameters["@CITY"].Value = CityTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@PHONE",
SqlDbType.VarChar,16));
dada.UpdateCommand.Parameters["@PHONE"].Value = PhoneTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@ADDR",
SqlDbType.VarChar,74));
dada.UpdateCommand.Parameters["@ADDR"].Value = AddressTB.Text.Trim();
dada.UpdateCommand.Parameters.Add(new SqlParameter("@EML",
SqlDbType.VarChar,64));
dada.UpdateCommand.Parameters["@EML"].Value = EmailTB.Text.Trim();
dada.UpdateCommand.ExecuteNonQuery();
conn.Close();


}
Could you tell me waths the problem here?
When I Push the Button Insert The following error appears:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the
current web request. Please review the stack trace for more information
about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set
to an instance of an object.

Source Error:

Line 104: SqlConnection conn = new SqlConnection("Data Source=BLEK;Initial
Catalog=Estate; User ID=blek; Password=banderas");
Line 105: SqlDataAdapter dada = new SqlDataAdapter("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);
Line 106: dada.UpdateCommand.CommandType = CommandType.Text;
Line 107: conn.Open();
Line 108: dada.UpdateCommand.Parameters.Add(new SqlParameter("@USRNAME",
SqlDbType.VarChar,20));


Source File: c:\inetpub\wwwroot\estates\pdata.aspx.cs Line: 106

Stack Trace:

[NullReferenceException: Object reference not set to an instance of an
object.]
Estates.pdata.InsertBtn_Click(Object sender, EventArgs e) in
c:\inetpub\wwwroot\estates\pdata.aspx.cs:106
System.Web.UI.WebControls.Button.OnClick(EventArgs e)

System.Web.UI.WebControls.Button.System.Web.UI.IPostBackEventHandler.RaisePo
stBackEvent(String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(IPostBackEventHandler
sourceControl, String eventArgument)
System.Web.UI.Page.RaisePostBackEvent(NameValueCollection postData)
System.Web.UI.Page.ProcessRequestMain()




Thank you very much!

Viktor


Define your parameters in the same sequence as they are used in the sql
query.

//Rutger
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Help with ASP.NET/ C# -> MSSQL 2
ASP.NET/C# Help 1
Code Error 16
Link from Repeater component 1
Error ... 4
SQL Server Insert Problem 1
Specified cast is not valid error 1
SP and C# problem 3

Top