Solved

C# html controls

Posted on 2011-03-11
11
261 Views
Last Modified: 2012-08-14
I want to use my drop down selected value and text box in an update statement.  So that my new query will be
"update gp_timesheetload  set "mydropdown value" = "textbox value"
based on my where_clause string




void ClickHandler(object sender, EventArgs e)
    {
 update statement goes here    
     }


string where_clause = "where company= '" + company_1 + "'and filename ='" + bach_1 + "' and weekending = '" + week_1 + "' and project='" + proj_1 + "'";
SqlCommand cmd = new SqlCommand("seLECT  distinct costcat from gp_timesheetload " + where_clause, cn);
cn.Open();
SqlDataReader rdr = cmd.ExecuteReader(CommandBehavior.CloseConnection);
rdr.Read();

<select name="weekending" style="font-size:8pt" tabindex="1" >
<%
        while (rdr.Read())
        {
 %>       
             <option><%=rdr[0].ToString()%></option><br><br><br><br>
<%            
        }

    }  
    
  
%>
</select>


<asp:TextBox id="tb1" runat="server" />
<asp:Button id="Button1" onclick="ClickHandler" runat="server" Text="Click here to submit"></asp:Button>

Open in new window

0
Comment
Question by:jaypappas
  • 6
  • 5
11 Comments
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35114495
Try this code.
You need to use ExecuteNonQuery() to execute an update statement
I understood that you need to update selected Dropdownlist value and TextBox value into separate columns in the table.
string connetionString = null;
SqlConnection cnn ;
SqlCommand cmd ;
string sql = null;

connetionString = "Data Source=ServerName;Initial Catalog=DatabaseName;User ID=UserName;Password=Password";
sql = "update gp_timesheetload  set dropdowncolumn = '" + myDropdown.SelectedValue + "', textboxColumn = '" + myTextBox.Text + "'";

cnn = new SqlConnection(connetionString);
try
{
    cnn.Open();
    cmd = new SqlCommand(sql, cnn);
    cmd.ExecuteNonQuery();
    cmd.Dispose();
    cnn.Close();
    MessageBox.Show (" ExecuteNonQuery in SqlCommand executed !!");
}
catch (Exception ex)
{
    MessageBox.Show("Can not open connection ! ");
}

Open in new window


Raj
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35114501
One more important thing.
You need to add WHERE condition also to the update statement. Otherwise it will update all the columns

Update the SQL Query line with your WHERE condition.
sql = "update gp_timesheetload  
set dropdowncolumn = '" + myDropdown.SelectedValue + "', 
textboxColumn = '" + myTextBox.Text + "' 
WHERE somecolumn = somevalue";

Open in new window


Note:- If required, backup your database before trying update statement.

Raj
0
 

Author Comment

by:jaypappas
ID: 35114503
How do I get the texbox and drop down value
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35114570
You mean read TextBox value and Dropdownlist value to SQL Query.

That's what I shown in my previous SQLQuery attached - Is it not working ?

Raj
0
 

Author Comment

by:jaypappas
ID: 35116262
My control is called weekending and set dropdowncolumn = '" + myDropdown.SelectedValue + "', is not working.  This is really my main problem.  And I need to do this on the function below

void ClickHandler(object sender, EventArgs e)
    {
 update statement goes here    
     }

Open in new window

0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35116787
I think you are not clear about previous comment.

The code I posted before - http:#a35114495 - put it this event
Also remember to replace the sample table name and column names I gave with your actual ones

Read once more my previous comments are let me know
Raj
0
 

Author Comment

by:jaypappas
ID: 35117383
where are you references weekending.  I dont see that nowhere.  I dont know how to get the value on the click event.  I am not sure you are clear on what I am asking.
0
 
LVL 23

Accepted Solution

by:
Rajkumar Gs earned 500 total points
ID: 35117405
That's what I said - replace the control names that I used with your real control name

For eg: If 'weekending' is the dropdownlist name, replace 'myDropdown' in my code with 'weekending'

Raj
0
 

Author Comment

by:jaypappas
ID: 35117642
I did replace and it dont work
0
 
LVL 23

Expert Comment

by:Rajkumar Gs
ID: 35119887
When you debug any error ?

Raj
0
 

Author Comment

by:jaypappas
ID: 35155992
Raj I think it did not work because I was using html control without runat = "server".  I was trying to mix asp and asp.net.  But what u gave me worked when I changed to pure asp.net controls
0

Featured Post

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

This article introduced a TextBox that supports transparent background.   Introduction TextBox is the most widely used control component in GUI design. Most GUI controls do not support transparent background and more or less do not have the…
This article is for Object-Oriented Programming (OOP) beginners. An Interface contains declarations of events, indexers, methods and/or properties. Any class which implements the Interface should provide the concrete implementation for each Inter…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This video explains how to create simple products associated to Magento configurable product and offers fast way of their generation with Store Manager for Magento tool.

706 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now