error on ExecuteNoQuery() - asp.net

i'am trying to update a row in a table using :
command.ExecuteNoQuery()
it's not giving me an error but it's not updating the row
This is my code :
Dim req As String = "Update Table Set Id= 5"
Dim cmd As New OleDb.OleDbCommand(req, connect())
cmd.ExecuteNonQuery()
disconnect()
thanks

Make sure that ID isn't an IDENTITY column.

the problem is that the query was too slow with MS Access.
Now it's working fine MS SQL Server.

Related

Getting an empty DataSet table from Oracle Client in .NET

Hi have this simple ASPX site setup. In the code behind, I'm connecting to an Oracle database, throwing a SELECT query and putting the result to a DataSet that I can then use/display on the website.
The problem: The query should give me result (works fine inside SQLDeveloper), but when I'm filling the DataSet, it gives me 0 rows every time. No Oracle errors show up, connection to the database opens up fine and the query looks correct.
I would have liked to get some kind of an Oracle error, that would have been easier to troubleshoot. I have tried to find a solution online, but haven't found anything that has helped with my problem.
Here is the relevant code I'm using:
using System.Data.OracleClient;
private OracleConnection conn = new OracleConnection();
-- // Below everyting is in the same method
var ds = new DataSet();
-- // conn.ConnectionString is corretly set
conn.Open(); // connection to database is opened
OracleCommand command = conn.CreateCommand();
var sql = "SELECT * FROM someTable"; -- // the SQL query
command.CommandText = sql;
OracleDataReader reader = command.ExecuteReader();
OracleDataAdapter adapter = new OracleDataAdapter(command);
ds.Tables.Clear();
adapter.Fill(ds);
Here the DataSet (ds) should be full, but it's always empty. Any pointers would be most welcome. Please let me know if I'm missing some information.

Accessing sql server temporary table from asp.net

What I am missing to get data from temporary table. This is showing error like Invalid object name #emp. Please help me
I am using Asp.net.
Dim sqlcmd = New SqlCommand("select * into #emp from employees", conn)
sqlcmd.ExecuteNonQuery()
sqlcmd = New SqlCommand("select * from #emp", conn)
Dim dr As SqlDataReader = sqlcmd.ExecuteReader
See Above query is working fine and data is going into temporary table. but it is not selecting again through second one query.
Thanks
Try to use Global Temporary table instead of Local Temporary tabel like.. ##emp
or
You can just use a stored procedure which has all the SQL statement you want to execute and return your desired recordset.

SQLite no such table in C#

I write this C# Code for insert one text data to my database table:
SQLiteConnection con = new SQLiteConnection("Data Source=tDB1.sqlite;Version=3;");
string q1 = "INSERT INTO tMembers(mName) VALUES(?)";
SQLiteCommand cmd1 = new SQLiteCommand(q1, con);
cmd1.Parameters.AddWithValue("#mName", txtName.Text);
con.Open();
cmd1.ExecuteNonQuery();
con.Close();
I copy my tDB1.sqlite to my program folder and Create tMembers table in sqlite.
when i run my program, program debugger show ""cmd1.ExecuteNonQuery();"" line and display to me: SQLite error no such table
can any help me?
thank you.
It is a little to weak look for a file based just on current directory. If you are sure the database is placed exactly in the same dir of the executable ( that is when debugging from visual studio $projectdir$\bin\debug you can enforce the path as in the following code:
SQLiteConnection con = new SQLiteConnection(string.Format("Data Source={0};Version=3;"
Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location),"tdb1.sqlite"))
);
Change the command string
string q1 = "INSERT INTO tMembers(mName) VALUES(?)";
to
string q1 = "INSERT INTO tMembers(#mName) VALUES(?)";
There is no need to keep corresponding to the column's name, but it must be the same with the args' name when executing the whole command. More info about this, you could search "PL/SQL" in google.
Besides, did you execute this command with "VALUES(?)"? I'm not sure that could work or not, you could execute INSERT INTO tMembers(#mName) DEFAULT VALUES instead, if there is no NOT NULL constraint.

Identity of recently added record and insert from gridview?

I am developing an ASP.Net VB Web Application
The application contains a GridView which displays the records of a user table from my created datable. The database is an Sql server database.
The code below inserts data into one table and through the built in function ##Identity to insert the most recently added record id (tt_id) from the trainingTbl table and inserting that record id into the userAssessmentTbl. Adding the identity to the second userAssessmentTbl table works fine.
Protected Sub Button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
Dim lblUsr2 As Control = FindControlRecursive(MultiTabs, "txtUsr")
Dim strQuery As String
Dim cmd As SqlCommand
strQuery = "Insert into trainingTbl(s_id, t_date, EditorId, wa_id, st_id) values(#s_id , #t_date, #EditorId, #wa_id, #st_id ) Insert into userAssessmentTbl(tt_id, UserId) values(##Identity, #UserId)"
cmd = New SqlCommand(strQuery)
cmd.Parameters.AddWithValue("#s_id", DDLCourse.Text)
cmd.Parameters.AddWithValue("#t_date", Convert.ToDateTime(txtDate.Text))
cmd.Parameters.AddWithValue("#EditorId", User.Identity.Name.ToString())
cmd.Parameters.AddWithValue("#st_id", myLblStation.Value().ToString)
cmd.Parameters.AddWithValue("#wa_id", myLblWatch.Value().ToString)
cmd.Parameters.AddWithValue("#UserId", lblUsr2.UniqueID.ToString)
InsertUpdateData(cmd)
End Sub
The issue I’m having seems to be centered on how I insert a uniqueidenifier from a GridView into a userAssessmentTbl database!
And how, I guess using a loop I can insert the UserId records from that Gridview (GridView1) into the userAssessmentTbl table along with the looped id from the ##Identity.
This part of the insert parameter seems to be incorrect:
cmd.Parameters.AddWithValue("#UserId", lblUsr2.UniqueID().ToString)
And the error I’m met with is: 'Conversion failed when converting from a character string to uniqueidentifier.'
I’ve also tried it like this:
cmd.Parameters.AddWithValue("#UserId", SqlDbType.UniqueIdentifier).Value().ToString()
And im met with the error: 'Operand type clash: int is incompatible with uniqueidentifier'
The qusetion has slightly changed to how do I Insert a String into SQL DB Where DataType Is Uniqueidentifier?
Any help will be really appreciated.
Well first of all:
##IDENTITY returns the most recently created identity for your current
connection, not necessarily the identity for the recently added row in
a table. Always use SCOPE_IDENTITY() to return the identity of the
recently added row.
Secondly, to asnwer your question:
The SQL type Uniqueidentifier and the CLR type Guid match up.
So instead of passing "#UserId" in as a parameter you need to create a Guid out of the string value.
Dim userID As Guid = New Guid(lblUsr2.UniqueID.ToString)

SQL Datatable error "Value cannot be null."

I am executing a sql query, and I am getting an error Value cannot be null.
Parameter name: dataTable
Code:
strSQLHost = "select HostBase.AppName from HostBase where HostBase.appid=0"
Dim dtHost As DataTable
Dim daHost As SqlDataAdapter = New SqlDataAdapter(strSQLHost, conn)
daHost.Fill(dtHost)
The error occurs at the daHost.Fill(dtHost)
When I run this query in SQL Enterprise manager, I get a value of 'None'. It's a valid value, not a null value.
How can I resolve this?
remove the last ' on your statement
I think it should read like this:
strSQLHost = "select Host.AppName from HostBase where HostBase.appid=0"
And instantiate your DataTable before passing it in:
Dim dtHost As DataTable = new DataTable()
select Host.AppName from HostBase where HostBase.appid=0
Seems like you're mixing table names when you only refer to one table: HostBase. You can't use table: Host in this query without including it in some sort of join (Even if it turned into a Cartesian Product) This is the change.
select HostBase.AppName from HostBase where HostBase.appid=0
Put a break and see the exact value of the string variable: strSQLHost

Resources