PC Review


Reply
Thread Tools Rate Thread

How to add a new record into database......

 
 
KKuser
Guest
Posts: n/a
 
      24th May 2004
Hello......

I know this might be a kind of "FAQ", but somehow I really can't find any
clue...(including MSDN)...

My code is as following...

Dim drow As DataRow
Dim ada As New SqlDataAdapter()
Dim ads = New DataSet()
Dim astrConn As String = "data source=(local);persist security
info=False;user id=" + UID + ";password=" + Pwd
Dim aconn = New SqlConnection(astrConn)
aconn.Open()

drow = ads.Tables("i_cust").NewRow
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
the compiler stops here and says:
"System.NullReference.Exception" occurred
Object variable or With block variable not set

Could anyone tell me what's wrong and how to correct it?

Thanks!!


 
Reply With Quote
 
 
 
 
Cor Ligthert
Guest
Posts: n/a
 
      24th May 2004
Hi KKuser,

I assume your code is complete and than the line is missing (see inline)

> Dim drow As DataRow
> Dim ada As New SqlDataAdapter()
> Dim ads = New DataSet()
> Dim astrConn As String = "data source=(local);persist security
> info=False;user id=" + UID + ";password=" + Pwd
> Dim aconn = New SqlConnection(astrConn)
> aconn.Open()


ada.fill(ads)

'the fill opens and close a connection automaticly (when the connection is
not already open), when you use one table it is more efficient to use that,
when you have to fill more tables in a dataset one after each other it is
more efficient to use the open and close.

> drow = ads.Tables("i_cust").NewRow


I hope this helps?

Cor


 
Reply With Quote
 
KKuser
Guest
Posts: n/a
 
      24th May 2004
Hi Cor:

Thank you for your kind reply!! I have modified my code as following:

Dim drow As DataRow
Dim ada As New SqlDataAdapter(cmd)
Dim ads = New DataSet()
Dim astrConn As String = "data source=(local);persist security
info=False;user id=" + UID + ";password=" + Pwd
Dim aconn = New SqlConnection(astrConn)
cmd = New SqlCommand("Select * from dbo.i_cust", aconn)
cmd.CommandType = CommandType.Text

aconn.Open()

cmd.ExecuteNonQuery()
ada.Fill(ads)
drow = ads.Tables("dbo.i_cust").NewRow
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
The compiler stops here again, and the error message is totally the same. By
the way, the "UID" and "Pwd" in the connection string are global variables
so there won't be any problem connecting to the database (I have checked
this).

Thanks again!!










"Cor Ligthert" <(E-Mail Removed)> 在郵件
news:(E-Mail Removed) 中撰寫...
> Hi KKuser,
>
> I assume your code is complete and than the line is missing (see inline)
>
> > Dim drow As DataRow
> > Dim ada As New SqlDataAdapter()
> > Dim ads = New DataSet()
> > Dim astrConn As String = "data source=(local);persist security
> > info=False;user id=" + UID + ";password=" + Pwd
> > Dim aconn = New SqlConnection(astrConn)
> > aconn.Open()

>
> ada.fill(ads)
>
> 'the fill opens and close a connection automaticly (when the connection is
> not already open), when you use one table it is more efficient to use

that,
> when you have to fill more tables in a dataset one after each other it is
> more efficient to use the open and close.
>
> > drow = ads.Tables("i_cust").NewRow

>
> I hope this helps?
>
> Cor
>
>



 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      24th May 2004
Hi K,

You can put the fill part in a try and catch block (that is normal for this
kind of operaions), than you see if there is a connection error or whatever
and what it is.

I show it to you. (What is that cmdexecute non querry that is to get a value
from your database directly, however it has probably nothing to do with your
error)

> aconn.Open()
>

try
> ada.Fill(ads)

Catch sqlExc As SqlException
MessageBox.Show(sqlExc.ToString)
Catch ex As Exception
MessageBox.Show(ex.Message)
Finally
Conn.Close()
End Try

> drow = ads.Tables("dbo.i_cust").NewRow
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>



 
Reply With Quote
 
KKuser
Guest
Posts: n/a
 
      24th May 2004
Hi Cor:

Thank you for your reply !!

In fact I did. I have put "Try" before
cmd.ExecuteNonQuery()
I just removed it when posting message on the mail.
There is no error metioned about
ada.Fill(ads)
, as I have said the compiler still stops at the same place......@@

The code is like this:

aconn.Open()

Try
cmd.ExecuteNonQuery()
ada.Fill(ads)
drow = ads.Tables("dbo.i_cust").NewRow
Catch ex as Exception
msgbox(ex.ToString)
End Try

**There seems to be no error of "SqlException"

"Cor Ligthert" <(E-Mail Removed)> 在郵件
news:%(E-Mail Removed) 中撰寫...
> Hi K,
>
> You can put the fill part in a try and catch block (that is normal for

this
> kind of operaions), than you see if there is a connection error or

whatever
> and what it is.
>
> I show it to you. (What is that cmdexecute non querry that is to get a

value
> from your database directly, however it has probably nothing to do with

your
> error)
>
> > aconn.Open()
> >

> try
> > ada.Fill(ads)

> Catch sqlExc As SqlException
> MessageBox.Show(sqlExc.ToString)
> Catch ex As Exception
> MessageBox.Show(ex.Message)
> Finally
> Conn.Close()
> End Try
>
> > drow = ads.Tables("dbo.i_cust").NewRow
> > ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
> >

>
>




 
Reply With Quote
 
Cor Ligthert
Guest
Posts: n/a
 
      24th May 2004
Hi KKuser,

I forgot an alterenative often made error, however that is easy so overcome,
the table name is case sensetive, so there is maybe no table with that name.
But when you use the zero index it is with one table always the right one.
(I added that in the corrections bellow)

However why is that cmd.ExecuteNonQuery, do I not know something, it is in
my opinion completly for nothing, only consuming time.

And you should choise or you delete that open or you add that finally
statement I did show you with the close. This has nothing to do with the
error, however is bad use of the connections from the SQL server.

I hope this helps?

Cor

See bellow.
>
> aconn.Open() ' to delete this is the best in this situation>
> Try
> cmd.ExecuteNonQuery() 'What does this do more than consuming

resources?
> ada.Fill(ads)
> drow = ads.Tables(0).NewRow
> Catch ex as Exception
> msgbox(ex.ToString)
> End Try




 
Reply With Quote
 
KKuser
Guest
Posts: n/a
 
      24th May 2004
Hi Cor:

I think the problem is almostly resolved. I think the key point should be
the code "Table("XXXX")" and "Table(0)".
And I also removed the code "cmd.ExecuteNonQuery"......I didn't have any
problem running the app, as you said...
Since I didn't give it enough information for those "non-null" fields, the
app is not completed yet...but I don't think it would be a problem.

Thank you for your reply, and your opinions are appreciated very much (and
of great help too). : )

"Cor Ligthert" <(E-Mail Removed)> 在郵件
news:(E-Mail Removed) 中撰寫...
> Hi KKuser,
>
> I forgot an alterenative often made error, however that is easy so

overcome,
> the table name is case sensetive, so there is maybe no table with that

name.
> But when you use the zero index it is with one table always the right one.
> (I added that in the corrections bellow)
>
> However why is that cmd.ExecuteNonQuery, do I not know something, it is

in
> my opinion completly for nothing, only consuming time.
>
> And you should choise or you delete that open or you add that finally
> statement I did show you with the close. This has nothing to do with the
> error, however is bad use of the connections from the SQL server.
>
> I hope this helps?
>
> Cor
>
> See bellow.
> >
> > aconn.Open() ' to delete this is the best in this situation>
> > Try
> > cmd.ExecuteNonQuery() 'What does this do more than consuming

> resources?
> > ada.Fill(ads)
> > drow = ads.Tables(0).NewRow
> > Catch ex as Exception
> > msgbox(ex.ToString)
> > End Try

>
>
>



 
Reply With Quote
 
 
 
Reply

Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
How do I insert a photo in a database record for that record only Chris Microsoft Access Getting Started 3 30th Dec 2007 08:17 PM
Clone a record, edit it and add as new record to database. tony_schullo@csx.com Microsoft Excel Programming 0 26th Oct 2007 04:34 PM
Cannot save record. Jet database engine cannot find a record =?Utf-8?B?SmFtZXMxOTU0?= Microsoft Access 0 12th Sep 2006 01:35 AM
EXPORT ACCESS TO SQL DATABASE RECORD BY RECORD... gssitaly via AccessMonster.com Microsoft Access VBA Modules 1 19th Jun 2006 04:14 PM
Copy fields from one record into another record in same database GINNY Microsoft Access Form Coding 2 15th Apr 2004 01:04 PM


Features
 

Advertising
 

Newsgroups
 


All times are GMT +1. The time now is 12:09 AM.