Please start any new threads on our new site at https://forums.sqlteam.com. We've got lots of great SQL Server experts to answer whatever question you can come up with.

 All Forums
 Development Tools
 ASP.NET
 Insert giving "Object Reference" error

Author  Topic 

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2010-03-12 : 09:45:31
I think I am losing it. I click the submit button and I get, "Object Reference not set to an instance of an object" and this line is highlighted in red:

cmd.Parameters.AddWithValue("@ReportName", txtName.Text)

What am I doing wrong?

Please see relevant code below. I can provide more if needed.
Thanks in advance for your assistance.

Dim cmd As New SqlCommand("INSERT INTO ReportQuestions(ReportShortName, ReportQTitle, ReportQOrder) VALUES (@ReportName, @ReportTitle, @ReportOrder)", con)
' cmd.CommandType = CommandType.StoredProcedure
cmd.Parameters.AddWithValue("@ReportName", txtName.Text)
cmd.Parameters.AddWithValue("@ReportTitle", txtTitle.Text)
cmd.Parameters.AddWithValue("@ReportOrder", txtOrder.Text)

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2010-03-12 : 11:23:23
try this

Dim cmd As New SqlCommand("INSERT INTO ReportQuestions(ReportShortName, ReportQTitle, ReportQOrder) VALUES (@ReportName, @ReportTitle, @ReportOrder)", con)

string name = txtName.Text.ToString();

cmd.Parameters.AddWithValue("@ReportName",name )



You have to use method overiding in c# to convert the value of the textbox to a string. as above. Repeat it for the other values
Go to Top of Page

simflex
Constraint Violating Yak Guru

327 Posts

Posted - 2010-03-12 : 11:41:21
Thanks a lot for your response.

First, I think you mistakenly didn't notice the fact I am using the vb flavor.

Second, it didn't work anyway as I am now getting:

same error -> "Object Reference not set to an instance of an object"

on this line -> string name = txtName.Text.ToString();

after I converted it to vb of course:

Dim name As String = txtName.Text.ToString()

Again, thanks
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2010-03-14 : 17:51:12
try this

Dim txt = txtName.Text.ToString()

command.Parameters.Add(New SqlParameter("@ReportName", SqlDbType.VarChar,8000))
command.Parameters(0).Value = txt
Go to Top of Page

jet1337
Starting Member

11 Posts

Posted - 2010-03-16 : 03:13:27
that works afrika thanks

ASPnix.com
Go to Top of Page

afrika
Master Smack Fu Yak Hacker

2706 Posts

Posted - 2010-03-18 : 10:21:09
glad2help
Go to Top of Page
   

- Advertisement -