Help with ASP.NET/ C# -> MSSQL

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
 
Z

Zürcher See

Viktor Popov 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:

Use a SqlCommand and not a SqlDataAdapter

SqlCommand sqlCommand = new SqlCommand("UPDATE BLEK.USERS SET
RealName=@NAME,UserCity=@CITY,UserPhone=@PHONE,UserAddr=@ADDR,UserEmail=@EML
WHERE Username=@USRNAME",conn);

sqlCommand.Parameters.Add( ...

bla bla bla

....
try
{
conn.Open();
sqlCommand.ExecuteNonQuerry();
}
catch(System.Exception e) { .... }
finally
{
if (conn.State!=ConnectionState.Closed) conn.Close();
}
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 SETRealName=@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
 
B

Ben Lucas

If you look at the documentation for the constructor you are using when
instantiating the DataAdapter, you should note that the string is the Select
Command. Hence, the UpdateCommand property has not been set and is null.
That is why line 106 fails.

It seems to me, though, that you do not need a DataAdapter at all. Why not
just create the Command object and use it. You're not really gaining
anything through the DataAdapter. In fact, you run all of your commands on
the Command property anyways.

--
Ben Lucas
Lead Developer
Solien Technology, Inc.
www.solien.com

Viktor Popov 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 SETRealName=@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
 

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

ASP.NET/C# Help 1
ASP.NET/C#->MS SQL help 2
Error ... 4
SP and C# problem 3
Code Error 16
Link from Repeater component 1
Help with SqlDataAdapter Update command (C#) 2
update db ? 9

Top