getting error when trying to execute an Sql command - asp.net

DataReader throwing an error message when trying to execute an command in my vb.net page
And the code throwing error is:
Dim connectionString As String
Dim connection As SqlConnection
Dim sql As String
connectionString = \\\connection string\\\
connection = New SqlConnection(connectionString)
sql = "select * from jb_jobs where city='Los Angeles' "
connection.Open()
Dim reader As SqlDataReader = sql.ExecuteReader()
And the error is:
'ExecuteReader' is not a member of 'string'
How to resolve this???

Try adding this:
sql = "select * from jb_jobs where city='Los Angeles' ";
var sqlCommand = new SqlCommand(sql, connection);
sqlCommand.Connection.Open();
var reader = sqlCommand.ExecuteReader();

Add this
connection.Open()
Dim cmd as new SqlCommand(sql,connection )
Dim reader As SqlDataReader = cmd.ExecuteReader()

Dim cmd As SqlCommand = new SqlCommand ();
cmd=(sql,connection);
cmd.CommandType=CommandType.Text;
Dim reader As SqlDataReader = cmd.ExecuteReader()

Related

Not able to access .DBF file in asp.net

I am not able read .dbf file in asp.net.
Error : External table is not in the expected format.
1) Using OLEDB
OleDbConnection conn = new OleDbConnection("Provider=Microsoft.Jet.OLEDB.4.0;Data Source=D:\\VisualFoxproDB;Extended Properties=dBASE 5.0;");
OleDbCommand command = new OleDbCommand("SELECT ID,NAME FROM table.dbf", conn);
conn.Open();
DataTable dt = new DataTable();
dt.Load(command.ExecuteReader());
gv1.DataSource = dt;
gv1.DataBind();
conn.Close();
.
2. Using ODBC
OdbcConnection conn = new OdbcConnection("Driver={Driver do Microsoft dBase (*.dbf)};DriverId=277;SourceType=DBF;SourceDB=D:\\VisualFoxproDB\\;Exclusive=No");
conn.Open();
string strQuery = "SELECT * FROM D:\\VisualFoxproDB\\test.dbf";
System.Data.Odbc.OdbcDataAdapter adapter = new System.Data.Odbc.OdbcDataAdapter(strQuery, conn);
System.Data.DataSet ds = new System.Data.DataSet();
adapter.Fill(ds);
return ds.Tables[0];

Not able to fetch data from sql database in asp.net web application using vb

I am using the below code to fetch data from SQL, I am not getting any error but code is not working on button click
Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
Dim command As New SqlCommand(strSQL, connection)
connection.Open()
reader As SqlDataReader = command.ExecuteReader()
While reader.Read()
GridView1.DataSource = reader
End While
'end connection and using close
I think you have to modify your code as follows,
Dim strSQL As String = String.Empty
strSQL = "SELECT * from jhg"
Using connection As New SqlConnection (ConfigurationManager.ConnectionStrings("xyz").ConnectionString)
Dim command As New SqlCommand(strSQL, connection)
connection.Open()
reader As SqlDataReader = command.ExecuteReader()
DataTable dt = new DataTable();
dt.Load(reader);
GridView1.DataSource = dt;
GridView1.DataBind();
You need to DataBind your GridView after providing the datasource:
GridView1.DataBind();

How to Use a parameter within SQL in Vb 2010 (web developer)

I am trying to work out SQL code in VB but I am having problems I have a simple database with the table admin with the columns UserName and Password.
I want to be able to read data from a text box and then input it into a SQL string… the SQL string works (I've tested it) and I can get it to output with a simple SELECT statement but I can't seem to get the SQL to read my Parameter.
Help?
Protected Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Call Password_Check(txtTestInput.Text)
End Sub
Public Sub Password_Check(ByVal Answer As String)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim parameter As New SqlParameter("#Username", Answer)
Try
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database1ConnectionString1").ConnectionString
con.Open()
cmd.Connection = con
cmd.CommandText = " SELECT Password FROM Admin WHERE (UserName = #Username)"
cmd.Parameters.Add(parameter)
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
Dim sothing As String
sothing = lrd("Password").ToString
If lrd("Password").ToString = txtPassword.Text Then
lblTestData.Text = "passwordSuccess"
ElseIf lrd("Password").ToString <> txtPassword.Text Then
lblTestData.Text = "passwordFail...:("
End If
End While
Catch ex As Exception
lblTestData.Text = "Error while retrieving records on table..." & ex.Message
Finally
con.Close()
End Try
End Sub
in your code above:
--> Dim parameter As New SqlParameter("#Username", Answer)
Can I suggest two options:
Dim parameter As New SqlParameter("#Username", sqldbtype.nvarchar)
parameter.value = Answer
or
cmd.CommandText = string.format("SELECT Password FROM Admin WHERE (UserName = {0})", Answer)
Full Code:
Public Sub Password_Check(ByVal Answer As String)
Dim con As New SqlConnection
Dim cmd As New SqlCommand
Dim parameter As New SqlParameter("#Username", SqlDbType.NVarChar)
parameter.Value = Answer
Try
con.ConnectionString = System.Configuration.ConfigurationManager.ConnectionStrings("Database1ConnectionString1").ConnectionString
con.Open()
cmd.Connection = con
cmd.CommandText = "SELECT Password FROM Admin WHERE (UserName = #Username)"
cmd.Parameters.Add(parameter)
Dim lrd As SqlDataReader = cmd.ExecuteReader()
While lrd.Read()
Dim sothing As String
sothing = lrd("Password").ToString
If lrd("Password").ToString = txtPassword.Text Then
lblTestData.Text = "passwordSuccess"
ElseIf lrd("Password").ToString <> txtPassword.Text Then
lblTestData.Text = "passwordFail...:("
End If
End While
Catch ex As Exception
lblTestData.Text = "Error while retrieving records on table..." & ex.Message
Finally
con.Close()
End Try
End Sub
Regarding to your Database system it is possible that it does not support parameter names. Have you tried ? Wat DB System you used?
cmd.CommandText = " SELECT Password FROM Admin WHERE (UserName = ?)"

getting the result of a sql statement in vs2010 vb.net

I am doing a sql statement to get all the data that starts with "A" in the database.
Sub searchMap()
Dim connectionString As String = ConfigurationManager.ConnectionStrings("WeddingPerfectionConnectionString").ConnectionString
Dim connection As SqlConnection = New SqlConnection(connectionString)
connection.Open()
Dim sql As String = "SELECT LocationName FROM MapSearch WHERE LocationName=" Like ("1%")
Dim command As SqlCommand = New SqlCommand(sql, connection)
Dim reader As SqlDataReader = command.ExecuteReader()
Dim customerId As Integer
If (reader.Read()) Then
customerId = reader.GetValue(0)
End If
reader.Close()
End Sub
And i would like to get the result and display it in a lable.
How can i go about doing it?
In SQL-Server:
Dim sql As String = "SELECT LocationID, LocationName FROM MapSearch WHERE LocationName LIKE 'A%'"
LIKE (Transact-SQL)

Update in ASP.NET

I am using this code snippet to update values in my database :
SqlConnection con = new SqlConnection(#"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True");
string str = "sama#yahoo.com";
SqlCommand com2 = new SqlCommand("select [user_Account] from User in str where [user_Email]=sama#yahoo.com", con);
SqlCommand com = new SqlCommand("update User set [user_Account]=? WHERE [user_Email=#em]", con);
com.Parameters.AddWithValue("user_Account",str);
com.Parameters.AddWithValue("#em",str);
con.Open();
com.ExecuteNonQuery();
com2.ExecuteNonQuery();
con.Close();
but I get this error
Incorrect syntax near the keyword 'User'.
Line 40: com.ExecuteNonQuery();
"User" is a reserved word in SQL. Wrap the name of the table in square brackets to specify that it's the name of something:
[User]
Why are you using two separate SqlCommand objects?? Absolutely not needed..... I would try to either UPDATE or SELECT - don't mix two totally separate operations into a single call....
Also: you should use parametrized queries to avoid SQL injection attacks, and you should put your SqlConnection and SqlCommand objects into using blocks - try this:
string updateStmt =
"UPDATE dbo.[User] SET [user_Account] = #AccountValue WHERE [user_Email] = #UserEMail;";
using(SqlConnection con = new SqlConnection(#"Data Source=SAMA-PC\SQLEXPRESS;Initial Catalog=advCenter;Integrated Security=True"))
using(SqlCommand _cmd = new SqlCommand(updateStmt, con))
{
_cmd.Parameters.Add("#AccountValue", SqlDbType.VarChar, 100).Value = str;
_cmd.Parameters.Add("#UserEMail", SqlDbType.VarChar, 100).Value = str;
con.Open();
_cmd.ExecuteNonQuery();
con.Close();
}

Resources