Solved

C# html controls

Posted on 2011-03-11
11
267 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article series is supposed to shed some light on the use of IDisposable and objects that inherit from it. In essence, a more apt title for this article would be: using (IDisposable) {}. I’m just not sure how many people would ge…
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…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
Many functions in Excel can make decisions. The most simple of these is the IF function: it returns a value depending on whether a condition you describe is true or false. Once you get the hang of using the IF function, you will find it easier to us…

911 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

25 Experts available now in Live!

Get 1:1 Help Now