Call data from database and change data in database.

How can I call the value of a field from a database and then use an "if" to do different things when the user clicks on a button?

 

Also, how can I then change that value of the field when I want to?

 

Thanks,

 

Sam 

0
airsam
2/28/2008 11:11:14 PM
asp.net.web-forms 93655 articles. 6 followers. Follow

8 Replies
837 Views

Similar Articles

[PageSpeed] 46

Could you please explain the question in detail.......

 

 

From the question, we can get the value by

 

OdbcConnection con = new OdbcConnection();

con.ConnectionString = ConfigurationManager.ConnectionStrings["ConnectionStringName"].ConnectionString;

con.Open();

OdbcCommand cmd = new OdbcCommand("SELECT ID FROM table1", con);

OdbcDataReader dr = cmd.ExecuteReader();

while (dr.Read())

{

User = Convert.ToInt32(dr["ID"]);

}

drUserno.Close();

cmdUserno.Dispose();

then you can use "if" for various options and use UPDATE command to update the value in the database...

0
dreamz
2/29/2008 1:31:51 AM

Yes need to explain clearly.


Shawpnendu Bikash Maloroy
http://shawpnendu.blogspot.com
0
shawpnendu
2/29/2008 5:42:10 AM

Sorry. I will be clearer.

 

I have made an extra field in my aspnetdb.mdf database (in the aspnet_Membership table) called HasVisited.

This is a bit field and by default is set to "False".

When the user (who is logged in) visits a specific page (ThePage.aspx) I want the "HasVisited" field to be changed to "True".

Also, when the user visits the Default.aspx page I want them to be directed to either "ThePage.aspx" or "Visited.aspx" based on whether the "HasVisited" field says they have or haven't visited.

The idea is the "ThePage.aspx" is an introduction page, which I only wan them to see once, and all subsequent visits I want them to go to the "Visited.aspx" page.

 

Thank you for your help,

Sam
 

0
airsam
2/29/2008 6:13:04 PM

You can place this HasVisited field in  in sessions variable  when user logs & check this it to redirect to appropiate page........

Session["HasVisited"] = DbValue;

// Can retrieve this as

if (Session["HasVisited"].ToString() == "1")

 // move to ThePage.aspx

else

 // move to Default.aspx

Hope I got your question right !!!

 


Thanks.

Please mark the post as answer if it helps you!!!





0
Mansoor
2/29/2008 6:52:54 PM

airsam :

Sorry. I will be clearer.

I have made an extra field in my aspnetdb.mdf database (in the aspnet_Membership table) called HasVisited.

This is a bit field and by default is set to "False".

When the user (who is logged in) visits a specific page (ThePage.aspx) I want the "HasVisited" field to be changed to "True".

Also, when the user visits the Default.aspx page I want them to be directed to either "ThePage.aspx" or "Visited.aspx" based on whether the "HasVisited" field says they have or haven't visited.

The idea is the "ThePage.aspx" is an introduction page, which I only wan them to see once, and all subsequent visits I want them to go to the "Visited.aspx" page.

 

Thank you for your help,

Sam



Here is an example snippet for you

private void CheckStatus()
{

    SqlConnection conn = new SqlConnection("Your Connection String")
     
            DataTable dt = new DataTable();
            string user = HttpContext.Current.User.Identity.Name;
                if (conn.State == ConnectionState.Open)
                { conn.Close(); }

                conn.Open();
                String sql = "SELECT HasVissited FROM MembershipTable WHERE UseId = '" +user+ "' "
                SqlCommand cmd = new SqlCommand(sql, conn); //But I would suggest yo use SP for security reasons
          
                cmd.ExecuteNonQuery();

            SqlDataAdapter ad = new SqlDataAdapter(cmd);
               ad.Fill(dt);
          
            if (dt.Rows.Count > 0)
            {
                    //Do the condition here
                   string visited = dt.Rows[0]["HasVissited"].ToString();

           if (visited == "1") //I assume that the HasVissited is BOOL type in your DB
           {
            //IF TRUE THEN DO SOMETHING HERE
           }
           else
           {//DO SOMETHING}

            }
            conn.Close();
              

    
}
protected void Page_Load(object sender, EventArgs e)
{

    if (!Page.IsPostBack)
    {
        CheckStatus();
    }
}


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
2/29/2008 7:03:02 PM

Isn't this C? I am programming it in Visual Basic. What do I need to do for VB?

 

Thanks,

 

Sam 

0
airsam
3/1/2008 10:39:02 AM

 Hi airsam,

Please try the following code, please call it at The page_load function. 

    Private Sub checkUser()
        
Dim conn As SqlConnection = New SqlConnection(strCon)
        conn.Open
        
Dim user As String = txtName.Text
        
Dim sql As String = ("select * from usertable where Name='"  _
                    + (user + 
"'"))
        
Dim cmd As SqlCommand = New SqlCommand(sql, conn)
        
Dim da As SqlDataAdapter = New SqlDataAdapter(cmd)
        
Dim ds As DataSet = New DataSet
        da.Fill(ds)
        
Dim HasVisited As String
        If 
(ds.Tables(0).Rows.Count > 0Then
            
HasVisited ds.Tables(0).Rows(0)("HasVisited").ToString
        
Else
            
Response.Write("there not exist the person.")
            
Return
        End If
        If 
(HasVisited "True"Then
            
Response.Redirect(VisitedPage, Path)
        
Else
            
Response.Redirect(ThePage, Path)
        
End If
        
conn.Close
    
End Sub

Let me know if I have misunderstood what you mean.
Thanks.
Hope it helps,
Hua Jun


Sincerely,
Hua Jun Li
Please remember to mark the replies as answers if they help and unmark them if they provide no help.
0
Hua
3/2/2008 9:49:13 AM

airsam:

Isn't this C? I am programming it in Visual Basic. What do I need to do for VB?

Hi airsam,

Sorry for the late reply.. anyways the sample snippet that i was provided  was actually written in C# language .. Here is the equivalent VB.NET codes for you..

Private Sub CheckStatus()
   
    Dim conn As New SqlConnection("Your Connection String")
   
    Dim dt As New DataTable()
    Dim user As String = HttpContext.Current.User.Identity.Name
    If conn.State = ConnectionState.Open Then
        conn.Close()
    End If
   
    conn.Open()
    Dim sql As String = "SELECT HasVissited FROM MembershipTable WHERE UseId = '" + user + "'"
    Dim cmd As New SqlCommand(sql, conn)
    'But I would suggest yo use SP for security reasons
    cmd.ExecuteNonQuery()
    Dim ad As New SqlDataAdapter(cmd)
    ad.Fill(dt)
   
    If dt.Rows.Count > 0 Then
        'Do the condition here
        Dim visited As String = dt.Rows(0)("HasVissited").ToString()
       
        If visited = "1" Then
                'IF TRUE THEN DO SOMETHING HERE
            'I assume that the HasVissited is BOOL type in your DB
        End If
           
    Else
    End If
    conn.Close()
   
End Sub 

Protected Sub Page_Load(ByVal sender As Object, ByVal e As EventArgs)  
    If Not Page.IsPostBack Then
        CheckStatus()
    End If
End Sub 


Regards,Vinz

"Code, Beer and Music" that's my way of being a programmer!

How to get your Forum Question Answered | Blog | CodeASP.NET
0
vinz
3/3/2008 1:06:13 PM
Reply:

Similar Artilces:

Web form that displays data and post data to a database
Hi,  I have a web form that needs to Get the userID and then display data from datasource 1 -  name, Phone, Address etc. of the user Then on the same form the user needs to submit additional information and click submit.  On click submit, I would like to capture all the info about the user from datasource1 - i.e name, phone, Address etc along wiht the additional info submitted by the user and insert into a table. Currently the way I am doing is: when the user enters their UserID, I use a formview to display the user data. When the user clicks submit on th...

C#.NET WEB APPLICATION
hei guys, i need help on my application, i'm using C#.NET as front-ed and SQL as back-end, aside from a datagrid, i want to display my data using textboxes and not a datagrid, i can do it using Visual Basic 6 but since i'm a newbee at C#.NET i don't know how to do it, specially the codes, can anyone help me with it? thank you in advance! If you are doing windows development, i would suggest using 'DataForm wizard' (Use Solution Explorer-->Rightclick project-->Add new Item-->New DataForm). It will guide you through steps. If you are doing web apps. Use quickstart to learn them. h...

Calling data from database and declaring variable to insert the data back into another table in database
Hello, I have called data from a database which is displayed on the screen (displays a particular item - then if the product is liked they click a buy button) then on the postback I declare the variables (as below) to be added back to a different table in my database, I have a problem in that it doesn't like the way I have declared the variable in bold (below is some of the code).  The variable is initially displayed from the database as decPrice and added to another table as decCartPrice on postback.  I get this error BC30469: Reference to a non-shared member requir...

Compare form data with data of a database
Hi @ allI programmed a login form which should compare the user-input with the data of a table of a database. I don't know exactly, how i should compare these two datas. Can anybody help me? I need only to know how to compare them. The stuff like the database connection and so on is already ok.....After that, I'll write a login-value into the session...thank you for your help You can check this threadhttp://forums.asp.net/thread/1445466.aspx Regards ASP.NET Hosting (HostingFest)...

Form Data to Database: Variable amount of data to be stored
Hello, all!  I have a form that I've built that has sort of a "sub-form" within it.  Basically, it's for an event calendar.  The form asks for a basic time frame, then asks the user to enter the event details.  The user can also click a button to add more fields for another event to the time frame, so submitting that page will need to be able to insert one or more events per time frame.  My question is this:  If a user enters multiple events, how can I make sure to loop through the data until it's all there?  Or better, is there a way ...

want to verify the data coming from a form with the data in database
--0-1949467889-1055227240=:39015 Content-Type: text/plain; charset=us-ascii i have a perl file which is receiving the data from a form...and i want to verify the two fields $a2 and $a1 i m receiving from form to be checked in a table in database( using mysql)...and then if the data is verified in any of the row of the table....the corresponding whole row data should be fetched. like if the table has columns lastname firstname age address person1 name1 23 add1 person2 name2 34 add2 person3.....and so on... and if $a...

How to Import the data form One database to another database ?
Hi All i populate 2 datawindows in diffrent databases(one is pardox another is delphi database) we r going to merge the 2 database table fields which r equal at that time i need this database to be stored to Third database when button click i want to import the data in third database(any other like oracle or access sybase) ThanQ vasu Vasu; Yes, this is possible, but first - How are you populating the first 2 DW's? Also, Is their structure (columns) the same? What release of PB are you using? regards ... Chris "venu" <pkkrao1@rediffmail.com...

Database to web form data entry?
I am definitely NOT a web programmer, but I think this should not be a gigantic project. There are 2 companies, company A has a database of client information. Company B has a web page for scheduling client services, and will not allow other methods of data entry into their system. Currently, company A has a clerk looking at their displayed client information and manually entering it into the Company B web page, and of course, making data entry errors in the process. What would it take to build a form that selects the client information from the Company A database and pas...

Insert Data from Database to Web form
 Hi,  I am trying to get the data from SQL Server Database and get the Result in Windows form. I am using Visual Web Developer 2008.I have written this code ..pls let me know what else I should do.Public Sub RetrieveDB(ByVal FirstName As String)        Dim conn As SqlConnection = GetConnection()        Dim dr As SqlDataReader        Dim myCommand As New SqlCommand("Retrieve_EmpTable", conn)        myCommand.CommandType = Data.Com...

substitute data from database into web form
Hello. Newbie here,  I have a field in a database called status with the values "I" or "A".  I need to display "Inactive" or "Active" in a gridview or formview web page.   How do I substitute these values.   Thanks in advance.mike Is this data being displayed in a datalist / datagrid?  You can either create a temp table in your sql or modify your code.  If you post your code I can show you how, otherwise you can do something like this in your sql: create table #temp(        act...

inserting data to text file from database and inserting data back to database from text file
Hello friends.... I am looking for 2 things(using c#.net or vb.net and sql svr 2000) 1.convert data from sql server 2000 database (say customers table from northwinds database) to a text file(separated by commas or just plain space) 2.Insert the data from text file back to database. Can someone pls give me the detailed code to achieve this....really need this on urgent basis.......Thank You.Gurjeet,England As for detailed code, I won't give you :P however I can tell you one way of doing this (there are multiple ways).The way I'd do it would be simple:Retrieve the information fr...

How to save Web form user data into a database
I have a web form the user enters information into multiple fields.  I want to capture all of the info that they enter into the fields on the form into a database once the user presses the submit button.  Any ideas on how to accomplish this would be great. Here's a sample snippet for youusing System.Text;private void InsertData(){using (SqlConnection conn = new SqlConnection("YOUR CONNECTION STRING");{        StringBuilder sb = new StringBuilder(string.Empty);            sb.AppendFormat(...

data, data, data
Hello, I need some advice on the best method to keep a database updated in this scenario. I have a local Solomon SQL Server where we keep our inventory. I want to build an application that will be hosted at a remote hosting location which provides a SQL database. I want to build the application to allow clients to access the inventory items and make requests based on the remote SQL data. The order will be sent to our fulfillment department via email; they will fiill the request and ship.  I'm not sure the method to do this while keeping the data current on the remote and the l...

4 Layered Web Application for Inserting data into a database using sql server as the back end and a web form as the front end using C#
4 Layered Web Application for Inserting data into a database using sql server as the back end and a web form as the front end using C# . Can someone provide with code as I am new to this architecture and framework. Better send email. Thanks In Advance, A New Bie I suggest taking a look at the Time Tracker Starter Kit (v1.x or x2.0) or the Issue Tracker Starter Kit (v1.x).  Full source code is available.  Click on the Downloads tab above to find them.  These are not exactly what you are looking for, but perhaps they will help you get started.Terri MortonEngagement Manag...

Web resources about - Call data from database and change data in database. - asp.net.web-forms

Database - Wikipedia, the free encyclopedia
A database is an organized collection of data . The data are typically organized to model aspects of reality in a way that supports processes ...

Database - Wikipedia, the free encyclopedia
... requiring information. For example, modelling the availability of rooms in hotels in a way that supports finding a hotel with vacancies. Database ...

Social Side of Code, Database CI and REST API Testing in Methods & Tools Winter 2015 issue
... for software developers, testers and project managers – has published its Winter 2015 issue that discusses the social side of code, database ...

Enterprises want to run databases in containers
More than three out of four IT decision makers are interested in running stateful applications like databases within containers, according to ...

Huge database of 191 million U.S. voters found on the open Internet
... such bulk information online, a recent discovery seems to indicate. According to VentureBeat , a security researcher found the exposed database ...

Top US School District Switches to Rimini Street for Oracle Database and Oracle E-Business Suite Support ...
Douglas County School District using savings from Rimini Street independent support to develop district-wide data visualization capabilities ...

IDG Contributor Network: Shrinking an already ginormous SharePoint database transaction log
Last month, I wrote about how to avoid having ginormous transaction logs within SharePoint databases. But what if it’s too late, and your files ...

18 million targeted voter records exposed by database error
A second database containing voter records has been exposed due to a configuration error. While the first held records that some dismissed inconsequential ...

Former Ohio cop uses cop database to stalk victim
... stalked her like an average person would, but stalked her in the way that only a police officer could: he stalked her utilizing a police database ...

U.S. Attorney Suggests Password Cardinals Used To Access Astros Scouting Database Was “Eckstein123"
... scouting director Christopher Correa plead guilty to five counts of unauthorized access to a computer for hacking the Houston Astros database. ...

Resources last updated: 2/5/2016 12:28:45 PM