|
when i use find method of DataRowCollection, it returns DataRow. i need to know the row number of this returned DataRow in the DataRowCollection. but i cant. how should encounter with this DataRow to find that number?
thanks
Rastegar
|
|
|
|
|
There is no IndexOf method either in DataTable.Rows or CurrencyManager.List. If your collection is huge you can use the following function
public int GetIndex(DataRow findRow) {
for (int i = 0; i < _dt.Rows.Count; i++) {
DataRow row = _dt.Rows[i];
if (row == findRow) {
return i;
}
}
return -1;
}
Alomgir Miah
Live Life King Size
|
|
|
|
|
The IndexOf method is available in .NET Framework 2.0.
|
|
|
|
|
dim conn as new oledb.oledbconnection("provider=microsoft.jet.oledb.4.0;user id=admin;data source = f:\emp.mdb")
conn.open()
DIM CMD AS NEW OLEDB.OLEDBCOMMAND("SELECT * FROM EMP",CONN)
DIM ADP AS NEW OLEDB.OLEDBADAPTER(CMD)
DIM DS AS NEW DATASET
DIM DT AS DATATABLE
DIM DR AS DATAROW
ADP.FILL(DS,"EMP")
DT=DS.TABLES("EMP"
DR=DT.NEWROW
DR("EMPNO")=TEMPNO.TEXT
DR("ENAME")=TENAME.TEXT
DR("SAL")=VAL(TSAL.TEXT)
DT.ROWS.ADD(DR)
ADP.UPDATE(DS,"EMP")
ADP.FILL(DS,"EMP")
CAN ANYONE PLEASE GUIDE ME IN THIS CODE, I WANT TO INSERT THE NEW RECORD IN TO THE TABLE
WITHOUT USING THE DATABASE CONTROL. THE ABOVE CODE IS NOT INSERTING THE VALUES INTO THE TABLE. HOW TO INSERT, UPDATE, DELETE RECORDS INTO THE TABLE WITHOUT USING DATABASE CONTROLS IN ADO.NET
|
|
|
|
|
Did you provide an InsertCommand command object for the dataAdapter? (retorical question, clearly you did not, judging from your code). The constructor you used only sets the SelectCommand command object for the dataAdapter. You still need to provide the insert and update ones...
btw YOUR CAPS KEY IS STUCK. THAT MAKES ME THINK THAT YOU ARE SHOUTING, which is rude...
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not.
Eric Hoffer
|
|
|
|
|
IMO you should update / insert only with stored procedures.
Here is an example of how I would code an insert statement.
Dim conn As New SqlConnection
conn.ConnectionString = Session("ConnectionString")
Dim cmd As New SqlCommand
cmd.Connection = conn
cmd.CommandType = CommandType.StoredProcedure
cmd.CommandText = "prc_Update_Notation_Types"
Dim Prm1 As New SqlParameter("@Original_Type", SqlDbType.VarChar, 50)
Prm1.Direction = ParameterDirection.Input
cmd.Parameters.Add(Prm1)
Prm1.Value = DBNull.Value
Dim Prm2 As New SqlParameter("@New_Type", SqlDbType.VarChar, 50)
Prm2.Direction = ParameterDirection.Input
cmd.Parameters.Add(Prm2)
Prm2.Value = Me.txtNotationType.Text
Dim Prm3 As New SqlParameter("@Type_Description", SqlDbType.VarChar, 50)
Prm3.Direction = ParameterDirection.Input
cmd.Parameters.Add(Prm3)
Prm3.Value = Me.txtNotationDescription.Text
conn.Open()
cmd.ExecuteNonQuery()
conn.Close()
"People who never make mistakes, never do anything."
My blog
http://toddsnotsoamazinglife.blogspot.com/
|
|
|
|
|
That, however, was not the question posted, and your answer sheds no light on the original problem.
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not.
Eric Hoffer
|
|
|
|
|
respected members thanks for ur response
one of the member was giving the sugession that we have to provoide the insert command. do you mean that i have to use (INSERT INTO) command of SQL at last to inserst rows to the table. when I use the SQL insert command the data's are getting added. I want to known whether there is any other method of storing data's in to the table without using SQL command or Data contorls to the forms.
i have given one statement DR=DT.NEWROW, this inserts an row is added to the datatable, it means that i am in iserting mode and atlast after moving the values to the datarow, the dataadapter is updated & filled. kindly guide me what showed be the actual procedure to be follwed to insert,update & delete records programatically with out using form controls.
|
|
|
|
|
Hi all
There are two ways to do this; either design the adapter and dataset at design time. If u design it in design time, system will automatically create update, insert & delete command for adapter.
System generated insertcommand for sql adapter :-
INSERT INTO Link_Policy(BatchID, TransNo, BarCodes) VALUES (@BatchID, @TransNo, @BarCodes); SELECT BatchID, TransNo, BarCodes FROM Link_Policy WHERE (BatchID = @BatchID) AND (TransNo = @TransNo)
Otherwise, create the adapter and create each sql command [just like select command] and assign it to adapter.
Ammu
|
|
|
|
|
The dataadapter has 4 different command objects, on each for select, update, insert, and delelte. The coinstructor you used creates and initializes the selectCommand object with the Select string you pass it. If it did not, then selection (and Fill) would fail. The other three are left up to you. You can either create them manually as new oledbCommand objects with the desired SQL for the command statement, or wrap the dataadapter in an oledbCommandBuilder, which will build the command objects for you (table must have a primary key for this to work). Alternatively, you can create the adapter in design mode, and add the insert (etc) comannds there.
This example from the MSDN docomentation shows how to use the oledbCommandBuilder:
OleDbConnection myConn = new OleDbConnection(myConnection);
OleDbDataAdapter myDataAdapter = new OleDbDataAdapter();
myDataAdapter.SelectCommand = new OleDbCommand(mySelectQuery, myConn);
OleDbCommandBuilder cb = new OleDbCommandBuilder(myDataAdapter);
myConn.Open();
DataSet ds = new DataSet();
myDataAdapter.Fill(ds, myTableName);
myDataAdapter.Update(ds, myTableName);
myConn.Close();
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
Hi All,
I execute the following code:
con.Open "server=(local);database=pubs;uid=sa;pwd=apwd;driver=sql server;app=testapp"
MsgBox "Click ok to close connection"
con.Close
Once I all the con.close, it takes approximately 1 minute for the connection to the sql server to be released. I have verified this by doing a sp_who2 in query analyser and by looking at connections in the perf mon.
I am having this problem with C++ using ADO.net via smartpointers, and C# using ado.net. The apps are not using connection pooling, and my simplest test above involves only the above code actually running, i.e. no other apps are running
Can anyone tell me why the connection is so slow to release, and how i can resolve this?
Thanks in advance,
Q
Quintes
|
|
|
|
|
With ADO (both System.Data.Oledb and System.Data.Sql namespaces) connection pooling is active by default, and your connection string does nothing to turn it off.
If you are using SQL server (which your connection string says you are) you should use the SQLClient class rather than the OledbClient. The SQL client is much more performant. See the framework documentation on connection string settings to control the pooling.
I'm not sure why you would want to disable the pooling, since this significantly improves performance on reopening connections with the same connection string...
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not.
Eric Hoffer
|
|
|
|
|
hi Rob,
Thanks for the reply.
Its not that I want to disable connection pooling, its just that I've taken it out of the equation. If fact I definitely want pooling.
VB and c++ are using ADO though, so while i understand and agree with you that I should be using the sqlclient class for c#, this doesnt have an effect on the VB and c++ as im using ADO and not ADO.net.
Any other ideas?
Q
Quintes
|
|
|
|
|
I'm confused. Are there 3 different apps, 1 VB (6?), one C++ (VC6?) and one C#?
If not, and all are VS2003 based, then all of them could use SqlClient namespace - VB.Net, C# both have access to this namespace, and C++ does as well through the managed classes (which can be mixed with unmanaged code like MFC if need be).
If they are a mix of legacy and new apps, then the older ones could continue to use ado (your present install must be putting the right stuff in...) and the new C# could move on to the SqlClient namespce stuff which is a different Oledb Provider. All have independant connection pools now anyway (even if the connection strings are the same, they are different processes, so each gets a new pool)...
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
guys, not sure why this one isn't working right, but i need to check the db for that info and if it is null push exception 1 if its not then verify the string saved on the table, if that doesn't pass then push exception 2
enclosed is the C# code....
dataConnection.ConnectionString = "user ID=sa; Pwd=password;Initial Catalog=mydb;Data Source=mycomputer";
dataConnection.Open();
dataCommand.Connection = dataConnection;
dataCommand.CommandText = "SELECT password FROM users WHERE username = 'dash' ";
dataReader = dataCommand.ExecuteReader(CommandBehavior.CloseConnection);
while (dataReader.Read())
{
if (dataReader["password"].ToString()!=null)
{
if (dataReader["password"].ToString().Equals("board"))
{
dataReader.Close();
}
else MessageBox.Show("Sorry, unable to verify license at this time \n\r Please try again later, or contact the system administrator.", "Incorrect License");
}
else MessageBox.Show("Sorry, We are unable to verify your license at this time. \n\r Please try again later, or contact the system admin.");
}
PS... I put a space between the user id and password becaues the browser was inputting a smiley!
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
First, since you are only expecting a single value, I would use ExecuteScalar instead of a DataReader:
dataCommand.Connection = dataConnection;
dataCommand.CommandText = "SELECT password FROM users WHERE username = 'dash' ";
try
{
object o = dataCommand.ExecuteScalar();
if(o == null)
{
...
}
else
{
string thePwd =(string)o ;
if(thePwd.Equals("myPwd"))
{
...
}
}
}
finally
{
dataConnection.Close();
dataConnection.Dispose();
}
....
Note that I did not use ToString() to convert the return value, but instead cast the object to a string. ToString is not strictly a conversion function, but instead returns the 'string representation' of an object. what ToString returns could be different from the 'value' of the object as a string. Also note that your comparison is case sensitive...
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
The opposite of the religious fanatic is not the fanatical atheist but the gentle cynic who cares not whether there is a god or not.
Eric Hoffer
|
|
|
|
|
Rob,
finally got a chance to plug this in and i'm having some problems....
i get a specified cast is not valid on the string thePwd = (string)o ; it looks like it is creating the thePwd as null rather than failing at the first if.
I've enclosed the whole try/catch block. also want to note that i WAS able to run the if statements properly if the data field was NOT null, but when i set it to null i get the cast not valid error. code is shown as tested, have not begun to add the commented areas yet.
thanks
try
{
object o = dataCommand.ExecuteScalar();
if(o == null)
{
InitializeComponent2();
dataConnection.Close();
dataConnection.Dispose();
}
else
{
string thePwd =(string)o ;
if(thePwd.Equals("board"))
{
dataConnection.Close();
dataConnection.Dispose();
InitializeComponent();
InitialTransAxs();
Connection();
}
}
}
catch (Exception e)
{
MessageBox.Show("(003) Sorry you are not authorized to view this material." +e.Message +e.StackTrace);
}
finally
{
dataConnection.Close();
dataConnection.Dispose();
}
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
Hmm, guess it returns a null for the object only if the record is not found, and an object wiith ans sqlNull value if the record is found but the field is null.
a simple solution woud be to change the cast to a 'silent fail' cast:
string thePwd = o as string;
if(thePwd != null)
{
if(thPwd.Equals("board")
{
dataconnection.Close(0);
....
}
}
else
Absolute faith corrupts as absolutely as absolute power
Eric Hoffer
All that is necessary for the triumph of evil is that good men do nothing.
Edmund Burke
|
|
|
|
|
k, i noticed you put the second if inside the first, was that intentional?
if so that will make my process a bit more difficult....
Dim Beautiful As String
Beautiful = "ignorant"
Label1.Text = "The world is full of " & Beautiful & " people."
Why is common sense such an un-common comodity?
|
|
|
|
|
if the 'as' cast fails, thePwd will be a null (uninitialized object) so the Equals() call would throw an exception, so I tested for this first.
A suggestion:
<pre>
bool loginOk = false;
string thePwd = o as string;
if( thePwd != null)
{
if( thePwd.Equals("board"))
{
loginOk = true;
}
}
if(loginOk == true)
{
// initialise your stuff
}
else
msgbox("can't Log in...");
</pre>
<small><b>Absolute faith corrupts as absolutely as absolute power<i>
Eric Hoffer</i></b></small>
<small><b>All that is necessary for the triumph of evil is that good men do nothing.
<i>Edmund Burke</i></b></small>
|
|
|
|
|
First of all i´d like to say Hi to all the members of this forum, and say thanks to the webmasters of this page; it´s a great one
My problem is the following:
I started developing VB.NET App about 5 months ago, and i am very concerned about database performance, because the software I develope at my job mainly involves using them.
I´ve been working on a project for about 2 months that uses an access database shared on a network folder (yep, that sucks i know) and i´d really like that someone tell me about a webpage or a book from where i can gain some expertise about that.
Currently, my app does one operation where it has to make several select and update commands, making the process very slow... While making that, the form of the app stutters, tried to use application.doevents, but was not very effective... should it be wise to create a thread and complete the operation in that thread?
As you see, i´m very newbie to this, and i feel a little lost... Hope that someone can lead me hand. Thanks a lot for reading, and sorry about my english
|
|
|
|
|
i need to clear all connection to the mssql server database via t-sql(the action that we can do by enterprise manager). because i want to restore database, but when it has some connection , i cant do that in my application. my application works in network and in the restore time, other clients may have transaction(s) on database. sql server doesnt allow to a client to restore but the restoring client is admin AND he realize to restore in this time, however other clients are working with database at the same time.
Thanks
Rastegar
|
|
|
|
|
Try this
ALTER DATABASE DatabaseName WITH ROLLBACK IMMEDIATE RESTORE DATABASE
DatabaseName FROM DISK = 'C:\DatabaseBackup.bak'
The ALTER DATABASE command will automatically kill all connections to the database being restored. This command prevents the need to query master.dbo.sysprocesses to kill existing connections in order to complete the restore.
Alomgir Miah
Live Life King Size
|
|
|
|
|
im so sorry but your code in query analyzer make these errors:
Server: Msg 156, Level 15, State 1, Line 1
Incorrect syntax near the keyword 'WITH'.
Server: Msg 102, Level 15, State 1, Line 1
Incorrect syntax near 'IMMEDIATE'.
could you please correct it and send me.
Thanks
Rastegar
|
|
|
|
|
Which version of SQL server you are using
Alomgir Miah
Live Life King Size
|
|
|
|
|