WriteValueInDataBase : Automation API

WriteValueInDataBase

Class library usage, coding and language questions.

WriteValueInDataBase

Postby omayer » Tue Apr 24, 2012 10:51 pm

user code module > any sample code or tutorial how to write value in sqldatabase, thank you in advance
beginner
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby Ciege » Tue Apr 24, 2012 11:57 pm

If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
Ranorex Guru
 
Posts: 1270
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Postby omayer » Wed Apr 25, 2012 12:56 am

Thank you Ciege, not sure why its not writing to db, using local db, i can read it but not able to write

public void AddCandidateName()
{

// Construct INSERT Command
_conn.Open();
SqlCommand cmd = new SqlCommand
("INSERT INTO RMQA(Name) VALUES ('aabb')",_conn);

_conn.Close();
}
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby Ciege » Wed Apr 25, 2012 1:13 am

I don't see an execute in your code to actually run the command...

Code: Select all
cmd.ExecuteNonQuery();
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
Ranorex Guru
 
Posts: 1270
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Postby omayer » Wed Apr 25, 2012 1:20 am

Thank you Ciege to look into the code, after the adding the line i getting following error
Invalid object name 'RMQA'.

public void AddCandidateName()
{

// Construct INSERT Command
_conn.Open();
SqlCommand cmd = new SqlCommand
("INSERT INTO RMQA(Name) VALUES ('Tjessem')",_conn);
cmd.ExecuteNonQuery();
_conn.Close();

}
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby Ciege » Wed Apr 25, 2012 1:32 am

Is RMQA valid?
What is your entire exception text?
Are you using SQL Authentication or Windows Authentication in your connection?
Did you include the initial catalog in your connection?
Did you GOOGLE for your specific error?
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
Ranorex Guru
 
Posts: 1270
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Postby omayer » Wed Apr 25, 2012 1:42 am

> Windows Authentication in my connection

> yes -initial catalog in my connection
>No- but going to -- GOOGLE for specific error?

Here is the new error i am getting --
Cannot insert the value NULL into column 'UniqueKey', table 'RMQA.dbo.Vendor'; column does not allow nulls. INSERT fails.
The statement has been terminated.

public void AddCandidateName()
{
SqlCommand dataCommand = new SqlCommand("INSERT Vendor (Name)VALUES ('first')",_conn);

_conn.Open();

dataCommand.ExecuteNonQuery();

_conn.Close();

}
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby omayer » Wed Apr 25, 2012 1:44 am

_conn =new SqlConnection("Data Source=(local)\\SQLExpress;Initial Catalog=RMQA;Integrated Security=SSPI");
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby Ciege » Wed Apr 25, 2012 3:33 am

So what it sounds like to me is you need to spend some time reading about how to interact with SQL from within C# (or you development language of choice).

Each of the errors you are getting or have gotten can be easily discovered by doing some simple Google searches and reading.

I hope that is not too harsh, but it is appearing that you want to have other people do your work for you without you showing any interest in trying to discover the issues yourself. I hope you can understand...
If this or any response has helped you, please reply to the thread stating that it worked so other people with a similar issue will know how you fixed your issue!

Ciege...
User avatar
Ciege
Ranorex Guru
 
Posts: 1270
Joined: Thu Oct 16, 2008 7:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Postby omayer » Wed Apr 25, 2012 3:26 pm

Thank you for your input, it worked after adding the primary key value , total time spent on google around 72hrs to learn the function that add value in sql,
Thank you,
Beginner
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby atom » Thu Apr 26, 2012 8:50 pm

Other comment...
Always use Command Parameters
It does the type conversion for you...
For example if you want to insert a DateTime into database, instead of converting it to a string add it as a command parameter!
atom
 
Posts: 339
Joined: Mon Dec 08, 2008 12:14 am
Location: Dublin, Ireland

Re: WriteValueInDataBase

Postby omayer » Fri Apr 27, 2012 5:24 pm

Atom would you plz show me how does the code look like "Command Parameters
It does the type conversion for you..."
Thank you in advance,
Beginner
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby atom » Fri Apr 27, 2012 5:38 pm

SQLite example:

Code: Select all
'connect
        objConnection = New SQLiteConnection(DbConnectionString)
        objConnection.Open()
        'update
        objCommand = New SQLiteCommand(objConnection)
        objCommand.CommandType = CommandType.Text
        objCommand.CommandText = "UPDATE " & cTableName & " SET LASTUSED=$LASTUSED WHERE NAME=$NAME AND VERSION=$VERSION AND RC=$RC AND PLATFORM=$PLATFORM"
        objCommand.Parameters.AddWithValue("$LASTUSED", DateTime.Now)
        objCommand.Parameters.AddWithValue("$NAME", Product.Name)
        objCommand.Parameters.AddWithValue("$VERSION", Product.Version)
        objCommand.Parameters.AddWithValue("$RC", Product.RC)
        objCommand.Parameters.AddWithValue("$PLATFORM", Product.Platform.ToString)
        objCommand.ExecuteNonQuery()
        'disconnect
        objCommand.Dispose()
        objConnection.Close()
        objConnection.Dispose()
atom
 
Posts: 339
Joined: Mon Dec 08, 2008 12:14 am
Location: Dublin, Ireland

Re: WriteValueInDataBase

Postby omayer » Fri Apr 27, 2012 6:17 pm

Atom, thank you so much for the sample code, i really needed this .
Thank you,
Beginner
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Re: WriteValueInDataBase

Postby omayer » Fri Apr 27, 2012 6:38 pm

Here is the scenario that i am trying to accomplish -

cycle one > test script produces some number like order id = 1112, I need to write this order id number in a empty row table called Result in sqlserver db.

the challenge i am facing in cycle two - when test execute in 2nd cycle - how to write orderid in new row instead of updating the existing field, same goes for 3rd cycle ...

Thank you in Advance
Beginner
Tipu
omayer
 
Posts: 428
Joined: Thu Oct 28, 2010 7:14 pm

Next

Return to Automation API

Who is online

Users browsing this forum: No registered users and 0 guests

cron