Solved

C# html controls

Posted on 2011-03-11
11
272 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
Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
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
 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In order to hide the "ugly" records selectors (triangles) in the rowheaders, here are some suggestions. Microsoft doesn't have a direct method/property to do it. You can only hide the rowheader column. First solution, the easy way The first sol…
The article shows the basic steps of integrating an HTML theme template into an ASP.NET MVC project
The Email Laundry PDF encryption service allows companies to send confidential encrypted  emails to anybody. The PDF document can also contain attachments that are embedded in the encrypted PDF. The password is randomly generated by The Email Laundr…

820 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