WriteValueInDataBase

Class library usage, coding and language questions.
omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

WriteValueInDataBase

Post by omayer » Tue Apr 24, 2012 9:51 pm

user code module > any sample code or tutorial how to write value in sqldatabase, thank you in advance
beginner
Tipu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Post by Ciege » Tue Apr 24, 2012 10: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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Tue Apr 24, 2012 11:56 pm

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

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Post by Ciege » Wed Apr 25, 2012 12: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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Wed Apr 25, 2012 12: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

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Post by Ciege » Wed Apr 25, 2012 12: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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Wed Apr 25, 2012 12: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: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Wed Apr 25, 2012 12:44 am

_conn =new SqlConnection("Data Source=(local)\\SQLExpress;Initial Catalog=RMQA;Integrated Security=SSPI");
Tipu

User avatar
Ciege
Ranorex Guru
Posts: 1335
Joined: Thu Oct 16, 2008 6:46 pm
Location: Arizona, USA

Re: WriteValueInDataBase

Post by Ciege » Wed Apr 25, 2012 2: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...

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Wed Apr 25, 2012 2: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

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: WriteValueInDataBase

Post by atom » Thu Apr 26, 2012 7: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!

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Fri Apr 27, 2012 4: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

atom
Posts: 357
Joined: Sun Dec 07, 2008 11:14 pm
Location: Dublin, Ireland

Re: WriteValueInDataBase

Post by atom » Fri Apr 27, 2012 4: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()

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Fri Apr 27, 2012 5:17 pm

Atom, thank you so much for the sample code, i really needed this .
Thank you,
Beginner
Tipu

omayer
Posts: 458
Joined: Thu Oct 28, 2010 6:14 pm

Re: WriteValueInDataBase

Post by omayer » Fri Apr 27, 2012 5: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