Can someone tell me what is wrong with my code here? - asp.net

strSql = "Select columnname from tbl where ID = '" & ViewState("v_INID") & "'"
v_ObjDs = v_ObjBREngine.FetchSqlDS(strSql)
name = v_ObjDs.Tables(0).Rows(0).Item(0)
ImageFrame.Attributes.Add("src", "..Drawings/folder/Drawings" + name)
DrawingName = Server.MapPath("~/Drawings/folder/Drawings/" & name)
If Not File.Exists(DrawingName) Then
name = "NoImageFound.jpg"
ImageFrame.Attributes.Add("src", "Images/" + name)
End If
Tell me what is wrong with my code. When I debug this piece of code, then DrawingName has an address. I just want to know that imageFrame syntax is correct or not.

System.IO.Path.GetFileName(DrawingName);
or
System.IO.Path.GetFileName(DrawingName);

Related

SQL Server Data type change from Numeric(7,3) to Varchar(20)

I have a field (dose_str) that needs to be changed from Numeric(7,3) to Varchar(20). I would like to know if there will be a need to change the query below (especially this portion SELECT (convert(varchar,cast(Prot_det.dose_str as float)) ) in the code of my application.
myCommand.CommandText = "
SELECT (convert(varchar,cast(Prot_det.dose_str as float)) + ' '
+ dose_unit + ' ' + dose_form_comment + ' ' + dose_mult) as Dose_str
from
Prot_det,
dosage_form
where
Protocol_num = '" & lblProtocol.Text & "' and
nsc_num = " & lstNSC.SelectedValue & " and
prot_det.dose_form = dosage_form.dose_form"
After changing the datatype of the column, you will be able to change this:
(convert(varchar,cast(Prot_det.dose_str as float))
to this:
(Prot_det.dose_str)
And I would recommend that you do.

While input of date - literal does not match format string

I have this line of code in asp.net through which inserting date into a table
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = " + ddlTrm.SelectedValue + "").Tables[0].Rows[0]["TRM_EFF_STDT"].ToString(),
and i am using oracle database . but while inserting data into it it show's an error
literal does not match format string
Is ddlTrm.SelectedValue string value? if it's true, I thing you should put value in quotes like this
"'" + ddlTrm.SelectedValue+"'"
Full example:
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = '" + ddlTrm.SelectedValue+"'").Tables[0].Rows[0]["TRM_EFF_STDT"].ToString()
I think you are wrong here ddlTrm.SelectedValue + "")
It should be ddlTrm.SelectedValue )
CMPI_EFF_DATE = cc.GetDataSet("SELECT TRM_EFF_STDT as TRM_EFF_STDT FROM TRM_MST WHERE TRM_CODE = " + ddlTrm.SelectedValue).Tables[0].Rows[0]["TRM_EFF_STDT"].ToString(),

Get results from two tables - asp.net/SQL Server 2008R2

I have a search form with 2 fields(first name and last name) from one table (Just has person's information) and 4 (Incident number, date, place, created by) from the other (has one or more incidents for the person in the first table) linked through foreign key(nameID). I think the problem is what kind of join to use and how to use the WHERE clause.
Thanks.
More information:
#Tim - Isn't the user input into one or more fields the filter or it is the WHERE Clause? The user doesn't have to fill in all the fields. Thats where I am getting lost. The user is trying to find the incident to update it. Does this help?
Also I have to use "Like%LName%" in the Where clause to get all the names if they don't enter the entire name.
My query looks like this:
Protected Sub BtnSearch_Click(sender As Object, e As EventArgs) Handles BtnSearch.Click
Dim strSearch As String
strSearch = "SELECT tblPatron.LName, tblPatron.FName, tblIncident.CreatedBy, "
strSearch = strSearch + "tblIncident.Inci_ID, tblIncident.Inci_date, tblIncident.Inci_type, tblIncident.Library, "
strSearch = strSearch + "tblIncident.PatronName, tblIncident.Location "
strSearch = strSearch + "FROM tblPatron INNER JOIN tblIncident ON tblPatron.PatronID = tblIncident.PatronID "
strSearch = strSearch + "WHERE "
strSearch = strSearch + "(tblPatron.LName Like '%" + txtLName.Text.ToString() + "%') "
strSearch = strSearch + "AND (tblPatron.FNAME Like '%" + txtFName.Text.ToString() + "%')"
strSearch = strSearch + "AND (tblIncident.Inci_ID ='" + strInciNum.Text.ToString() + "')"
strSearch = strSearch + "AND (tblIncident.Inci_date = '" + txtInciDate.Text.ToString() + "')"
strSearch = strSearch + "AND (tblIncident.Inci_type = '" + ddInciCat.SelectedValue.Trim + "')"
strSearch = strSearch + "AND (tblIncident.Library = '" + ddLib.SelectedValue.Trim + "')"
SearchPDS.SelectCommand = strSearch
SearchPDS.DataBind()
GridSearchResults.DataBind()
GridSearchResults.Visible = True
End Sub
do this:
SELECT A.FirstName, A.LastName, B.IncidentNumber, B.Date, B.Place, B.CreatedBy
FROM Name A INNER JOIN Incident B
ON A.NameID = B.NameID
SELECT firstname, lastname, incidentnumber, date, place, createdby
FROM name n
INNER JOIN incident i ON n.nameID = i.nameID
WHERE firstname LIKE '%'+#firstname+'%'
AND lastname LIKE '%'+#lastname+'%'
Where #firstname and #lastname are parameters containing values from the search fields
In your string concatenation style, just add txtFName.Text.ToString() and txtLName.Text.ToString() into the string in place of those parameters.
I took the suggestion of logixologist. On the click event I added multiple if statements to check for the null value and then add build the query string. At the same time I made one of the dropdown to be a default value instead of "Select" and that would be my starting Where parameter. This works for me now. There might be a better way of writing the query, I am just beginner with asp.net
Thanks for all your replies. I love this forum.
What you need is dynamic sql. Basically you start by declaring a varchar(max)
DECLARE #Sql as varchar(max)
Then you will set it to the base SQL Statement:
SET #SQL = 'SELECT A.FirstName, A.LastName, B.IncidentNumber, B.Date, B.Place, B.CreatedBy
FROM Name A INNER JOIN Incident B
ON A.NameID = B.NameID where lastname IS NOT null ' -- PUT IN A WHERE CLAUSE THAT WILL ALWAYS BE TRUE
---Here is the concept in pseudo code
IF #lastname is not null
BEGIN
SET #SQL = #SQL + 'and lastname = '%' + #lastname + '%'
END
IF #FIRSTNAMEis not null
BEGIN
SET #SQL = #SQL + 'and FIRSTNAME = '%' + #FIRSTNAME+ '%'
END
At the end
EXEC (#SQL)
This will give you any option they put in.

How to set dynamically the correct Dateformat in VB for SQL Query

Hello i have an sql query which uses a Dateformat. This code will run on different servers but every server have different dateformats. sometimes yyyy-MM-dd and sometimes yyyy-dd-MM.
I tried to read the userlanguage to choose the correct query. but it doesnt work properly.
Do you know any other good solution to solve my problem ?
Thanks in advance
Dim Systemsprache as String
Systemsprache = Request.UserLanguages(0)
If String.Compare(Systemsprache, "de-DE") = 0 Then
sqlcmd = "SELECT convert(varchar(8) , [VON],108) as [VON] ,convert(varchar(8) , [BIS],108) as [BIS] FROM [RESERVIERUNGRAUM] where RAUM_ID =" + hCurrRaumID.Value + " and VON >='" + Date.Parse(wiDateVON1.Value).ToString("yyyy-MM-dd") + "' and BIS <'" + Date.Parse(wiDateVON1.Value).AddDays(1).ToString("yyyy-MM-dd") + "'"
Else
sqlcmd = "SELECT convert(varchar(8) , [VON],108) as [VON] ,convert(varchar(8) , [BIS],108) as [BIS] FROM [RESERVIERUNGRAUM] where RAUM_ID =" + hCurrRaumID.Value + " and VON >='" + Date.Parse(wiDateVON1.Value).ToString("yyyy-dd-MM") + "' and BIS <'" + Date.Parse(wiDateVON1.Value).AddDays(1).ToString("yyyy-dd-MM") + "'"
End If
If I remember correctly, you can use the universal format '#yyyy-mm-dd#' regardless of the server default date format.

query string in classic asp code to catch integer values

I have written below code piece that is supposed to search from DB based on two Parameters
Integer value(ID of a SHIP)
String value(SHIP name).
But with the following code I am able to get data from the DB using the SHIP name i.e using the 2nd case. But unable to search the data using the ID of the SHIP.
Following is the code snippet. Any help on these is highly appreciated.
nIMO = sql_ship_friendly(request.querystring("nIMO"))
if IsNumeric(nIMO) = false then
nIMO = ""
else
nIMO = cInt(nIMO)
end if
sVessel = sql_ship_friendly(UCase(request.querystring("sVessel")),10)
if not nIMO = "" then
'search based on vessel id
sql = "SELECT IMO_NBR, VESSEL_NM, COALESCE(SHIP_TYP,'0') AS SHIP_TYP, COALESCE(DWT_WT, 0) AS DWT_WT, COALESCE(YEAR_BUILT_NBR, 0) AS YEAR_BUILT_NBR FROM RSP_VESSEL_VW WHERE ACTIVE_IND='Y' AND IMO_NBR = 7723948"
Set db1 = Server.CreateObject("ADODB.Connection")
db1.Open GV_VIEW_DB_String
Set rs = db1.Execute(sql)
Set dbl = nothing "
elseif not sVessel = "" then
'search based on vessel number
sql = "SELECT IMO_NBR, VESSEL_NM, COALESCE(SHIP_TYP,'0') AS SHIP_TYP, COALESCE(DWT_WT, 0) AS DWT_WT, COALESCE(YEAR_BUILT_NBR , 0) AS YEAR_BUILT_NBR FROM RSP_VESSEL_VW WHERE VESSEL_NM LIKE '"&SVESSEL&"%' AND ACTIVE_IND='Y'"
' response.write sql Set db1 =
Server.CreateObject("ADODB.Connection") db1.Open GV_VIEW_DB_String
Set rs = db1.Execute(sql) Set dbl = nothing
if your first case you have placed following
sql = "SELECT IMO_NBR, VESSEL_NM, COALESCE(SHIP_TYP,'0') AS SHIP_TYP,
COALESCE(DWT_WT, 0) AS DWT_WT, COALESCE(YEAR_BUILT_NBR, 0) AS
YEAR_BUILT_NBR FROM RSP_VESSEL_VW WHERE ACTIVE_IND='Y' AND IMO_NBR =
7723948"
but I can see you are using the nIMO perameter in query. Put it and you should get result..
You can use it like AND IMO_NBR = nIMO or likewise.
The problem is there is no where on your SQL statement that use nIMO variable as your search criteria. Forget about using IsNumeric(nIMO) = false... Use isNull function instead. Try this.
if not isnull( request.querystring("nIMO")) then
nIMO = cInt(nIMO)
sql = "SELECT IMO_NBR, VESSEL_NM, COALESCE(SHIP_TYP,'0') AS SHIP_TYP," &_
"COALESCE(DWT_WT, 0) AS DWT_WT, COALESCE(YEAR_BUILT_NBR, 0) AS " &_
"YEAR_BUILT_NBR FROM RSP_VESSEL_VW WHERE ACTIVE_IND='Y' AND " &_
"IMO_NBR =" & nIMO
...
...
...
end if

Resources