i want to Select Values ex(Date,Time....)from SQLServer on the PageLoad & shows Them in The Many Labels. i try this code but in all labels shows the Time . i want to show All Values not one value in all labels . Please help me .
string strquery = "select Time,Date,SeatPrice,EventName from Event_SingleReservation";
SqlConnection connection2 = DBConnection.getConnection();
connection2.Open();
SqlCommand cmd2 = new SqlCommand();
cmd2.Connection = connection2;
cmd2.CommandText = strquery;
string eventname = cmd2.ExecuteScalar().ToString();
lbl1_EventName.Text = eventname;
string eventdate = cmd2.ExecuteScalar().ToString();
lbl2_EventDate.Text = eventdate;
string eventtime = cmd2.ExecuteScalar().ToString();
lbl3_EventTime.Text = eventtime;
string seatprice = cmd2.ExecuteScalar().ToString();
lbl_seatpriceshow.Text = seatprice;
The ExecuteScalar() selects only one value from the first column - i.e. using it against select Time,Date,SeatPrice,EventName from Event_SingleReservation will return only Time which is the first column.
To select all values you should use ExecuteReader()
SqlDataReader reader = cmd2.ExecuteReader();
if (reader.Read())
{
lbl1_EventName.Text = reader[0];
lbl3_EventDate.Text = reader[1];
...
}
See What is the difference between ExecuteScalar, ExecuteReader and ExecuteNonQuery?
Related
i have to store a select query result in a variable .i'm new in asp.net . i used executescalar but it doesn't work. i try many times but i failed here my last try :
using (SqlConnection sqlConnection = new SqlConnection())
{
var connetionString = ConfigurationManager.ConnectionStrings["connections"].ToString();
sqlConnection.ConnectionString = connetionString;
string sql = "Select sum((prime_comptant+10)*0.12) from mvt_garantie_quittance where numero_quittance='" + numQuittance + "'";
SqlDataAdapter adapter = new SqlDataAdapter(sql, sqlConnection);
DataSet dataset = new DataSet();
adapter.Fill(dataset);
string result = dataset.Tables[0].ToString();
}
Can you fix the code to me? i have to store the result in a variable
string sql = "Select sum((prime_comptant+10)*0.12) from mvt_garantie_quittance where numero_quittance='" + numQuittance + "'";
var connetionString = ConfigurationManager.ConnectionStrings["connections"].ToString();
string result = null;
using (SqlConnection conn = new SqlConnection(connetionString))
{
SqlCommand cmd = new SqlCommand(sql, conn);
conn.Open();
result = cmd.ExecuteScalar().ToString();
}
When I am using the syntax
Update table set #column1=#value1, #column2=#value2
Using cony As New SqlConnection(connectionstring)
cony.Open()
Dim kktarget As String = "target" + yymmc.ToString
Dim kkactual As String = "actual" + yymmc.ToString
Dim query As String = "update tpmkpi set #kktarget =#tg1,#kkactual=#ac1 where rno=#knox"
Using cmdy As New SqlCommand(query, cony)
cmdy.Parameters.Add("#kktarget", SqlDbType.Char).Value = kktarget
cmdy.Parameters.Add("#kkactual", SqlDbType.Char).Value = kkactual
cmdy.Parameters.AddWithValue("#tg1", tg1)
cmdy.Parameters.AddWithValue("#ac1", ac1)
cmdy.Parameters.AddWithValue("#knox", knox)
Dim resulty As Integer = cmdy.ExecuteNonQuery()
The update staement should be like this,
update tpmkpi set kktarget =#tg1,kkactual=#ac1 where rno=#knox
I am modifying a webpage. Webpage is using visual basic and my database is a SQL database.
The webpage gives me the total Sick, Comp, and Vacation time off. Right now, I get the logon name of whoever's logged onto the computer, and I look up the EmployeeID in a tblEmployees so I can search tblTimeAvailable by the EmployeeID, and then add up the different times.
I want to also get the StartDate from tblEmployees so I can also search by anniversary date (after I calculate it from the Start Date). I'm getting an error "Value of type 'Integer' cannot be converted to 'Date' on the rvsd=command2.ExecuteNonQuery line. Here's my code:
Dim rve As Object
Dim rvsd As Date
Dim dt As Date = Today
'Get network login name (name only)
split = windowsLoginName.Split("\".ToCharArray)
vname = split(1)
'Get employeeid from table that matches login name
Dim Connection As String = "Data Source=WillSQL\ict2;Initial Catalog=timesql_testing;Integrated Security=SSPI"
Using con As New SqlConnection(Connection)
Dim sqlemp As String = "SELECT EmployeeID FROM tblEmployees where login = #loginname"
Dim sqlstdt As String = "SELECT StartDate FROM tblEmployees where login = #loginname"
Dim command As New SqlCommand(sqlemp, con)
Dim command2 As New SqlCommand(sqlstdt, con)
con.Open()
command.Parameters.Add(New SqlParameter With {.ParameterName = "#loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
command2.Parameters.Add(New SqlParameter With {.ParameterName = "#loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
rve = command.ExecuteScalar
rvsd = command2.ExecuteNonQuery
End Using
Dim theDateThisYear As Date
theDateThisYear = DateSerial(Year(Now), Month(rvsd), Day(rvsd))
If theDateThisYear < Now() Then
theDateThisYear = DateAdd("yyyy", 1, theDateThisYear)
End If
Dim NextAnniversary As Date = theDateThisYear
MsgBox(NextAnniversary)
'Get Sick Time - DOES NOT INCLUDE CHECKING TO MAKE SURE LESS THAN ANNIV DATE YET
Using con As New SqlConnection(Connection)
Dim sqls1 As String = "Select SUM(NoofHours) as Total from tblTimeAvailable where workcode = 1 and EmployeeID = #emp"
Dim command As New SqlCommand(sqls1, con)
con.Open()
command.Parameters.Add(New SqlParameter With {.ParameterName = "#emp", .SqlDbType = SqlDbType.NVarChar, .Value = rve})
rvsa = command.ExecuteScalar
End Using
Thanks in advance for any help you can give me!
Assuming that the column [StartDate] in the database has a type of DateTime then all you need to do is
rvsd = CDate(command2.ExecuteScalar)
Although you could have retrieved both values with one query:
SELECT [EmployeeID], [StartDate] FROM [tblEmployees] where [login] = #loginname"
and so
Dim rve As String
Dim rvsd As Date
Dim connStr As String = "Data Source=WillSQL\ict2;Initial Catalog=timesql_testing;Integrated Security=SSPI"
Dim sql = "SELECT [EmployeeID], [StartDate] FROM [tblEmployees] where [login] = #loginname"
Using conn As New SqlConnection(connStr)
Using cmd As New SqlCommand(Sql, conn)
cmd.Parameters.Add(New SqlParameter With {.ParameterName = "#loginname", .SqlDbType = SqlDbType.NVarChar, .Value = vname})
conn.Open()
Dim rdr = cmd.ExecuteReader()
If rdr.HasRows Then
rdr.Read()
rve = rdr.GetString(0)
rvsd = rdr.GetDateTime(1)
End If
End Using
End Using
string date = ddlShowDates.SelectedValue.ToString();
cmd = new SqlCommand("SELECT tbl_Shows.ShowTime FROM tbl_Shows INNER JOIN tbl_MovieTimings ON tbl_Shows.ShowId = tbl_MovieTimings.ShowId WHERE tbl_MovieTimings.Date='" + date + "'", con);
I want to display show time in dropdownlist as per date is selected.
Always use sql-parameters instead of string concatenation to prevent sql-injection.
I guess you have a second DropDownList which should be filled from the first:
DateTime date = DateTime.Parse(ddlShowDates.SelectedValue);
string sql = #"SELECT tbl_Shows.ShowTime
FROM tbl_Shows
INNER JOIN tbl_MovieTimings
ON tbl_Shows.ShowId = tbl_MovieTimings.ShowId
WHERE tbl_MovieTimings.Date=#Date";
using(var con = new SqlConnection("ConnectionString"))
using(var cmd = new SqlCommand(sql, con))
{
cmd.Parameters.Add("#Date", SqlDbType.Date).Value = date;
con.Open();
using(var rd = cmd.ExecuteReader())
{
while(rd.Read())
{
TimeSpan time = rd.GetTimeSpan(0);
timeDropDownList.Items.Add(time.ToString());// change format as desired in TimeSpan.ToString
}
}
}
Public Shared Function GetData(ByVal id As Integer) As List(Of SomeClass)
Dim command As New OracleCommand
Dim conn As New OracleConnection(WebConfigurationManager.ConnectionStrings("ConnectionString").ToString)
Dim param As New OracleParameter
param.ParameterName = "idnumber"
param.Value = id
param.DbType = DbType.Int32
command.Parameters.Add(param)
command.Connection = conn
command.CommandText = "select VAL1, VAL2, Year from local.proc where id = :idnumber and Year = to_number(to_char(sysdate,’YYYY’))"
Dim reader As OracleDataReader
Dim someList As New List(Of SomeClass)
connection.Open()
reader = command.ExecuteReader()
While reader.Read
Dim someClass As New SomeClass
someClass.VAL1 = reader("VAL1")
someClass.VAL2 = reader("VAL2")
someClass.Year = reader("YEAR")
someList.Add(someClass)
End While
connection.Close()
Return someList
End Function
This function is giving an ORA-00911 invalid character error. I have other methods of the same style and these are functioning correctly. Any advise on where I am going wrong here please? Thanks
I think the issue is with the wrong quotes in the SQL statement for the year i.e. : ’YYYY’
Change it to 'YYYY'
Replace the line:
command.CommandText = "select VAL1, VAL2, Year from local.proc where id = :idnumber and Year = to_number(to_char(sysdate,’YYYY’))"
with
command.CommandText = "select VAL1, VAL2, Year from local.proc where id = :idnumber and Year = to_number(to_char(sysdate,'YYYY'))"
It looks like you are using formatted apostrophes.
to_char(sysdate,’YYYY’
try change it to
to_char(sysdate,'YYYY'