SQL statement and If statement

I've problem, it's in sqlcommand it's in "where" condition, which if it's exist in sql the "ELSE" not work, and if the condition not in sqlcommand the "ELSE" appear, could someone help

 

it's in

selectcommand.CommandText = "SELECT Weekno FROM Weeks where Weekno='" + Weekno.Text + "'"

if i delete the condition the " ELSE " appear. otherwise it's not appear

whole code

Dim myconnection As New SqlConnection("Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\football.mdf;Integrated Security=True;User Instance=True;MultipleActiveResultSets=True ")

myconnection.Open()

Dim insertcommand As New SqlCommand

Dim selectcommand As New SqlCommand

Dim myreader As SqlDataReader

insertcommand.Connection = myconnection

insertcommand.CommandType = CommandType.Text

selectcommand.Connection = myconnection

selectcommand.CommandType = CommandType.Text

 

selectcommand.CommandText = "SELECT Weekno FROM Weeks where Weekno='" + Weekno.Text + "'"

myreader = selectcommand.ExecuteReader

If myreader.Read Then

 

If (Weekno.Text = myreader.Item("Weekno")) Then

Response.Write("already Exsit")

Else

Response.Write("New")

End If

End If

 

 

 

0
Inuyashaheart
10/11/2008 10:18:01 PM
asp.net.sql-datasource 29906 articles. 0 followers. Follow

6 Replies
841 Views

Similar Articles

[PageSpeed] 23

What exactly you're trying to do with this code? Your reader loop should be WHILE not IF.


Beware of bugs in the above code; I have only proved it correct, not tried it.
(Donald Knuth)
0
Naom
10/12/2008 1:26:23 AM

 Your code seems to ask sql to return what was passed in.

 

Perhaps better would be 'select count(*) where weekno = @weekno.

If the reader returns 0 then the record does not exist, 

then

sorry C# dialect

int result = int.Parse(rdr[0].ToString());

if(result > 0)

{

 //record exists

}

else

{

//does not exist

}


Mark in St. Pete, FL
icq:421115712
0
pogo2346
10/12/2008 1:44:11 AM

Thanks you brothers

but here am not trying to count the record.. idea is

i want to check the record if the same number the came from weekno.text are =(equal) to weekno in record if it's than it tell him that already exist if not in record count as new one..

i hope it's clear

thank so much for contributes..

Sad

 

0
Inuyashaheart
10/12/2008 8:41:06 AM

With your select statement you already receive records with WeekNo = weekno.text  So, if your select returns 1 or more records, then you have records in your database with this number, otherwise you don't. That's why the previous reply with a count was a good one.


Beware of bugs in the above code; I have only proved it correct, not tried it.
(Donald Knuth)
0
Naom
10/12/2008 10:57:05 AM

Hi,

    You have to pass the weekno.text to the query as parameter and this can be used in the where clasue.

Fi this condition comes to true means there is a record in the table otherwise there were no records in the table.

Following is the modified code:

selectcommand.CommandText = "SELECT Weekno FROM Weeks where Weekno='" + Weekno.Text + "'"

myreader = selectcommand.ExecuteReader

If myreader.Read Then

       Response.Write("already Exsit")

Else

       Response.Write("New")

End If

Hope this helps you. You can acheive the same using count(*) as mentioned in the previous thread.


-Sri
-------------------------------------------------
If this post was useful to you, please mark it as answer. Thank you!
0
ksridharbabuus
10/12/2008 11:07:08 AM

Hi, thank you.. but in such case what to I should do

myCommand.CommandText = "SELECT CardSerialN,cardamount FROM card where CardSerialN='" & TextBox1.Text & "'"

myreader = myCommand.ExecuteReader

If myreader.Read Then

If (TextBox1.Text = myreader.Item("CardSerialN")) Then

If (DropDownList1.SelectedValue > myreader.Item("cardamount")) Then

 

Response.Write(
"Not enoght amount")

Else

Response.Redirect("Ansureness.aspx")

 

End If

Else

Response.Write("No number exist") End If

Else

MsgBox("Wrong number ", MsgBoxStyle.Exclamation)

End If

which have multicases first user pass parameter "cardserial" if it's exsit ok, than also check the amount that user want to add it if it's bigger that exist in card wont allow it's less ok."general idea"

what happand here every condiation work unless

Response.Write("No number exist") 

---

so my question how i let the "ELSE" appear which happaned to me in both cases

And another thing it's I setup my database to take the date from Calander but i want to add the time also how do I do that

the record is smalldatetime type"10/3/2008 12:00:00 AM".. so how ?

 

Thanks you, and sorry for disturbance

0
Inuyashaheart
10/13/2008 1:03:51 PM
Reply:

Similar Artilces:

If Statement combined with SQL statement.
Hi  This is going to sound like quite  a strange question. Basically what I am trying to do is use Membership.GetUser().UserName to get the current user who is logged in and I want to see if that user exists in a table on my database before adding some data to the labels. I figured it should go in the page load part. The name of the table is userteams. Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) If Membership.GetUser().UserName exists in Userteams Then goallie.Text = Select player FROM Userteams Where Username = Membership.getUser().Use...

SQL Logging SQL Statement
Hi at All!! I use ASA 7 so I'd like to know if there is a way to logging the sql instruction that database server dbsrv7, receive from clients ? I need this because a client that use odbc connection after performing an insert into a table, the client receive the Message: "SQL truncate". I need to know the field on the table that generate this warning!! Thanks in advance Massimo The tool that does this is called "request-level logging" and in V7 you have to enable it from the command line when you start the server, using the -zr (and optionally -zo) c...

Adding an IF Statement to the SQL Statement
I have the following tables and SQL statment which works out a league table for me from the given results. I am now trying to add a bit more to this: If the Walkover column = 1 then that row will be a home walkover meaning that the score will be 0(Home Score) - 0(Away Score) but the home team will recieve 3 points, and away team recieves 0 points, win column will be 1 and the played colum will be 1 as well.If the Walkover column = 2 then that row will be a home walkover meaning that the score will be 0(Home Score) - 0(Away Score) but the away team will recieve 3 po...

Viewing a SPIDs last SQL statement using sqltext()...though sometimes only partial SQL statements are shown
The dbcc sqltext() command seems to allow you to view the last SQL statement issued by an active SPID on the server. However I've noticed that sometimes only part of the SQL statement is returned - does anyone know why this might be, and if so whether it's due to a server configuration. Anyway, here's my notes on how to view the last SQL statement processed: ** How to see the last SQL a SPID has processed -- dbcc traceon(3604) Directs trace output to the client, rather than to the error log 1> dbcc traceon(3604) 2> go DBCC execution completed. If DBCC pr...

Please Help me:SQL statement failed: (-301) Internal database error ??? -- transaction rolled back SQL statement failed: (-101) Not connected to a database
Hi all, I am using Adaptive Server Anywhere 8 version 8.0.2.4239. I am able to sync successfully between 2 desktops with the same version. When I am trying to sync with my pocket pc device version 8.0.4122, Towards the end of Synchronization (nearly 95% completed) ,I am getting this error "E. 05/29 19:28:31. SQL statement failed: (-301) Internal database error ??? -- transaction rolled back E. 05/29 19:28:31. SQL statement failed: (-101) Not connected to a database" The log file contains E. 05/29 19:28:31. SQL statement failed: (-301) Internal database er...

SQL Anywhere 11 hanging on SQL statement
I have a 800mb DB that I unloaded/reloaded from version 8 to the Developer Edition of version 11. If I open ISQL and run: alter dbspace SYSTEM add 100 MB the DB file immediately grows by 100MB, but the execution of the statement never finishes. However if I use Sybase Central to create a DB and then perform the same procedure on this new empty DB, the statement executes in under 30 seconds. If I try pressing the "stop" button in ISQL, this locks up that program. And if I press the "Shutdown" button on the SQL Anywhere console window, the server also...

Dynamic SQL Format 2 SQL statement
Hi , I tried to implement Dynamic SQL Format 2 SQL statement but it is running fine with out error but it is not saving data to data base INT Dept_id_var =3d 156 String Dept_name_var SetNull(Dept_name_var) PREPARE SQLSA FROM "INSERT INTO dept VALUES (?,?) using my_transaction " ; EXECUTE SQLSA USING :Dept_id_var,:Dept_name_var ; [1] Commit using my_transaction ; [2] To test I tried my_transaction.autocommit =3d true my SQLSA satatement =85. my_transaction.autocommit =3d false but in both case I am not able to insert data to database...

TreeView (Difficulty in SQL within SQL statement)
I have difficulty in getting results of the Query that is within another query in Tree View Structure. The code is as: ourConnection = New OdbcConnection("Driver={Microsoft Access Driver (*.mdb)}; DBQ=C:\ReportWritingTool\ekindb") ourConnection.Open() ourSQLQuery = New OdbcCommand("SELECT DISTINCT SISY, SISYR, DRDL01, SIFUNO, SIMD, SIOBNM FROM F0005, F9860 WHERE (F0005.DRKY = TRIM(F9860.SISY)) AND (F9860.SIFUNO='TBLE' OR F9860.SIFUNO='BSVW'AND F0005.DRSY='98' AND F0005.DRRT='SY');", ourConnection) ourDataAdapter = New Odb...

Executing the i-sql statements using i-sql interface.
I am writing the c/c++ interface which will execute i-sql query statements to retrive information from the isql. It involves parsing the isql output to get the required information objects ( database, devices, ...etc). This parsing string method seems to be not appropriate, as the format of the isql statement might change in future release of ASEs. I am wondering is there any otherway I can access the information(like querying info of databases, devices, ...etc) from sybase server from "C/C++" program in structural manner??. Is it possible to get the return status(succe...

sql statement limit in Execute SQL Task
i have two machines. i was working in a "Execute SQL Task" object's SQL window on a rather long sql task on one machine and reached some kind of limit on the length of the sql statements. i can not add another line of code. i cut and pasted this same code into the exact same "Execute SQL Task" object's SQL window on the second machine and it does not have this limit. does anyone know what causes this? (in fact....i could paste it in twice - doubling the length)   What do you mean by "i cut and pasted this same code into the exact same screen on the se...

sql statement datename and not in equivalent to linq statement
Hi,     How can I translate this sql syntax statement to a linq to sql codes?? I like to separate records that are in first group, monday to saturday but date not in holidays table, and in second group, either sunday or date in holidays table...  /*Sql statement */ /*Mon to Sat group and not a holiday*/ Select * From Deliveries where DateName(dw,DeliveryDate) != 'Sunday' and DeliveryDate Not In (Select Date From Holidays) /*Sun & holiday group*/ Select * From Deliveries where DateName(dw,DeliveryDate) = 'Sunday' or DeliveryDat...

RFC: SQL Extensions for SQL::Statement [Long]
I will be releasing a significantly upgraded SQL::Statement and DBD::File shortly and I have some questions about interface. I'd really appreciate some feedback. These are the features that are near finalization: * heterogeneous SQL across multiple DBI sources * per-table DBI connections on a single dbh * improved API for DBD writers * support for in-memory tables ala DBD::RAM for all SQL::Statement DBDs * improved ability to specify SQL dialect features on a per-DBD, per-session, or per-statement basis * user-supplied functions, procedures, and persistent ...

sql statement in vb.net
hi there, i've a question about building an sql-statement in vb.net. the statement i tried in the sql server , there is everything ok. sql: EXEC crosstab 'select KategorieB from Planungsdaten group by Kategorie ', 'sum(PDAnzahl)', 'PerDatum', 'Planungsdaten', 'Planung = 8 and Periode in (select Periode from getperiodenplanung(8))' now how can i put this into my vb-code?? i tried it the same way like in sqlserver but i got an error : error: "wrong syntax near the END Keyword" please help thanks Patrick ...

Sql Datasource select statement
I have a gridview that uses a sqldatasource to display (classid, userid, startdate, enddate) from a class table.  My datagrid also contains a submit button.  When this button is clicked I am inserting this data into a new table called register.  I need a way to not show classes for the user if that user is already in the register table for that class.  Currently all items are being shown which would allow the user to register for the same class more than once.  Any ideas?   Thanks  you could make classid and userid as a pri...

Web resources about - SQL statement and If statement - asp.net.sql-datasource

User:Jimbo Wales/Statement of principles - Wikipedia, the free encyclopedia
As we move forward with software and social changes, I think it is imperative that I state clearly and forcefully my views on openness and the ...

Category:Articles containing potentially dated statements from July 2011 - Wikipedia, the free encyclopedia ...
This is an administration category . It is used for administration of the Wikipedia project and is not part of the encyclopedia. It contains ...

Peyton Manning Issues Statement Denying Doping Allegations - Bleacher Report
Denver Broncos quarterback Peyton Manning has issued a statement strongly denying that he received human growth hormone from an anti-aging clinic ...

Madonna says Sean Penn never assaulted her in court statement, denying previous reports
... of the allegations that have surfaced over the years accusing Sean of incidents of physical assault and abuse against me," she said in a statement ...

Statement from Premier League’s Crystal Palace Football Club
Crystal Palace Football Club is delighted to announce that it has today signed a deal with investors Josh Harris and David Blitzer. The deal ...

What I like to see: Agencies Issue Statement on CRE Loans
... doesn't suggest a problem in CRE lending, rather the agencies are trying to get ahead of future problems. From the FDIC: Agencies Issue Statement ...

Chicago police statement on double fatal police shooting - Chicago Tribune
Chicago police emailed the following statement to the media at about 9:45 the morning of Dec. 26, 2015, after an early morning police-involved ...

What changed in new Fed statement
This is a comparison of today's FOMC statement with the one issued after the Fed's previous policy-making meeting on Oct. 28.

Does Draymond feel Warriors have chance to make statement vs Cavs?
... Giants A's Sharks Warriors Kings 49ers Raiders Quakes Insiders More Tickets Shop Watch Does Draymond feel Warriors have chance to make statement ...

Rajon Rondo's Statement Looks A Lot Like Kobe Bryant's Statement
... pay after he called referee Bill Kennedy a “faggot” in a Dec. 3 game . (Kennedy publicly came out after the incident.) Rondo tweeted a statement, ...

Resources last updated: 12/27/2015 9:48:58 PM