How I can use sessions in my code using asp.net vb.net -
i want make sessions, don't know can start. logged in myself, without using session , have make sessions. below code have done far.
protected sub btnlogin_click(byval sender object, byval e eventargs) handles btnlogin.click if txt_username.text = "" or txt_password.text = "" error_usr_invalid.visible = false if txt_username.text = "" error_usr_blank.visible = true elseif txt_username.text <> "" error_usr_blank.visible = false end if if txt_password.text = "" error_pwd_blank.visible = true elseif txt_username.text <> "" error_pwd_blank.visible = false end if elseif txt_username.text <> "" , txt_password.text <> "" dim con new sqlconnection(configurationmanager.connectionstrings("connectionstring").connectionstring) con.open() dim cmd new sqlcommand("select * users username = '" + txt_username.text + "' , password = '" + txt_password.text + "'", con) cmd.parameters.addwithvalue("@username", txt_username.text) cmd.parameters.addwithvalue("@password", txt_password.text) dim da new sqldataadapter(cmd) dim dt new datatable() da.fill(dt) if dt.rows.count > 0 error_usr_invalid.visible = false error_usr_blank.visible = false error_pwd_blank.visible = false response.redirect("main.aspx") else error_usr_invalid.visible = true error_usr_blank.visible = false error_pwd_blank.visible = false end if end if
please me out suggesting ideas in code.
you can read session here : asp.net session state overview
now, there few examples :
add value session : session.add("username", txt_username.text)
get value session : dim username string = session("username")
for example : can store username session("username")
, use on other page (for example in main.aspx
) until remove it.
one more example :
default.aspx
protected sub btnlogin_click(byval sender object, byval e eventargs) handles btnlogin.click session.add("username", txt_username.text) response.redirect("main.aspx") end sub
and in main.aspx :
protected sub page_load(byval sender object, byval e system.eventargs) handles me.load label1.text = session("username") end sub
session can used storing arrays, , other type of datas. example :
session.add("list", list(of string)) session("list").add("record #1") session("list").add("record #2") session("list").add("record #3")
and use somewhere else :
for x = 0 session("list").count - 1 label1.text = session("list")(x) + ", " next
remove session : session.remove("username")
(this session item removed)
when finished work (for example log out), can remove, clear, abandon session.
session.removeall() : session.clear() : session.abandon()
you can store data session , use anywhere in web app.
btw. in code, little better approach :
dim con new sqlconnection(configurationmanager.connectionstrings("connectionstring").connectionstring) if con.state = connectionstate.open con.close() con.connectionstring = configurationmanager.connectionstrings("connectionstring").tostring con.open() dim cmd new sqlcommand("select * users username = @username , password = @password;", con) cmd.parameters.addwithvalue("@username", txt_username.text) cmd.parameters.addwithvalue("@password", txt_password.text) dim tds sqldatareader = cmd.executereader session.add("isexist", tds.hasrows) 'there example how store result session (in case result value true or false) tds.close(): cmd.dispose(): con.close() 'avoid connection stay opened if session("isexist") = true 'user successful log in, session("isexist") true session.remove("isexist") response.redirect("main.aspx") else 'username or password doesn't exist in database, session("isexist") false session.remove("isexist") 'do want end if
of course, have pay attention session
timeout
.... can read more provided link on start of post.
Comments
Post a Comment