Click here to Skip to main content
15,442,754 members
Home / Discussions / C#
   

C#

 
AnswerRe: Printing Bytes to printer in MVC Pin
Kornfeld Eliyahu Peter8-Jul-14 0:14
professionalKornfeld Eliyahu Peter8-Jul-14 0:14 
GeneralRe: Printing Bytes to printer in MVC Pin
nitin_ion8-Jul-14 1:15
Membernitin_ion8-Jul-14 1:15 
AnswerRe: Printing Bytes to printer in MVC Pin
Kornfeld Eliyahu Peter8-Jul-14 1:20
professionalKornfeld Eliyahu Peter8-Jul-14 1:20 
AnswerRe: Printing Bytes to printer in MVC Pin
Richard MacCutchan8-Jul-14 1:14
mveRichard MacCutchan8-Jul-14 1:14 
GeneralRe: Printing Bytes to printer in MVC Pin
nitin_ion8-Jul-14 1:16
Membernitin_ion8-Jul-14 1:16 
GeneralRe: Printing Bytes to printer in MVC Pin
Pete O'Hanlon8-Jul-14 1:33
mvaPete O'Hanlon8-Jul-14 1:33 
AnswerRe: Printing Bytes to printer in MVC Pin
Dave Kreskowiak8-Jul-14 5:06
mveDave Kreskowiak8-Jul-14 5:06 
QuestionWindows services Not starting with c# codes Pin
Chinedu Nwankwo7-Jul-14 21:04
MemberChinedu Nwankwo7-Jul-14 21:04 
Hi


please need urgent help , i am try to resart windows services , using a dropdownlist to choose the particular service, each time i choose the particular service and click restart button i get the error : (Particular service stopped)



public partial class Homepage : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!Page.IsPostBack)
{
FillServiceName();
}
}

protected void SqlDataSource1_Selecting(object sender, SqlDataSourceSelectingEventArgs e)
{

}

protected void DropDownList1_SelectedIndexChanged(object sender, EventArgs e)
{
//Label1.Text = DropDownList1.SelectedIndex.ToString();
//Label1.Text = DropDownList1.SelectedIndex.ToString();
}
public void ServiceRestart(string nameofservice)
{
//Get the name of the service from the Web.config
// string nameofservice = ConfigurationManager.AppSettings["service1"];
ServiceController sv = new ServiceController(nameofservice);

//Get service timeout from App.Config file
int timeinmilliseconds = int.Parse(ConfigurationManager.AppSettings["timeinmilliseconds"].ToString());

try
{
if (sv.Status.Equals(ServiceControllerStatus.Running) && sv.CanStop)
{
int ms1 = Environment.TickCount;
int ms2;
TimeSpan timeout = TimeSpan.FromMilliseconds(timeinmilliseconds);
try
{
sv.Stop();
sv.WaitForStatus(ServiceControllerStatus.Stopped, timeout);
//Console.WriteLine("Service " + nameofservice + " stopped");
Label2.Text = "Services " + nameofservice + " stopped";
ms2 = Environment.TickCount;
timeout = TimeSpan.FromMilliseconds(timeinmilliseconds - (ms2 - ms1));
}
catch
{
Label2.Text = "Services " + nameofservice + "failed to stoped";
}

try
{
sv.Start();
Label2.Text = "Services" + nameofservice + "started";
}
catch
{
Label2.Text = "Services" + nameofservice + "failed to start";
}
sv.WaitForStatus(ServiceControllerStatus.Running, timeout);
//Console.WriteLine("Service " + nameofservice + " started");
//Label2.Text="Services" + nameofservice + "started";
}
else
{
sv.Start();
//Console.WriteLine("Service " + nameofservice + " started");
Label2.Text = "Services" + nameofservice + "started";
}
}

catch (Exception e)
{
//Console.WriteLine("An error has occured: " + e.StackTrace);
//Label2.Text = "Services" + nameofservice + "stopped";
Label2.Text = "An error has occured: " + e.Message;
//Re-start the service but with a longer timeout period
//ServiceRestartAgain(nameofservice);

}

}


//Another service
public void ServiceRestartAgain(string nameofservice1)
{
//Get the name of the service from the Web.config
//string nameofservice1 = ConfigurationManager.AppSettings["service1"];
ServiceController sv1 = new ServiceController(nameofservice1);


int timeinmilliseconds1 = int.Parse(ConfigurationManager.AppSettings["timeinmilliseconds2"].ToString());

try
{
if (sv1.Status.Equals(ServiceControllerStatus.Running) && sv1.CanStop)
{
int ms11 = Environment.TickCount;
TimeSpan timeout1 = TimeSpan.FromMilliseconds(timeinmilliseconds1);
sv1.Stop();
sv1.WaitForStatus(ServiceControllerStatus.Stopped, timeout1);
//Console.WriteLine("Service " + nameofservice1 + " stopped");
Label2.Text="Service " + nameofservice1 + "stopped";

int ms22 = Environment.TickCount;
timeout1 = TimeSpan.FromMilliseconds(timeinmilliseconds1 - (ms22 - ms11));
sv1.Start();
sv1.WaitForStatus(ServiceControllerStatus.Running, timeout1);
//Console.WriteLine("Service " + nameofservice1 + " started");
Label2.Text="Service " + nameofservice1 + "started";
}

else
{
sv1.Start();
//Console.WriteLine("Service " + nameofservice1 + " started");
Label2.Text = "Service " + nameofservice1 + " started";
}
}

catch (Exception e)
{
string stacktrace = e.StackTrace;
//Console.WriteLine("An error has occured in restarting the service again: " + stacktrace);
Label2.Text = e.Message;
//Send mail to e-channels support(In the body of the mail put the stacktrace after adding the message. This is for tracking purposes.
// MailEChannelsSupport(stacktrace);
}

}
public void FillServiceName()
{
try
{
SqlConnection con = new SqlConnection(ConfigurationManager.ConnectionStrings["ATMrestartserviceConnectionString"].ConnectionString);



con.Open();
SqlCommand cmd = new SqlCommand("SELECT * FROM Service", con);

cmd.ExecuteNonQuery();
SqlDataAdapter adap = new SqlDataAdapter(cmd);
DataSet dats = new DataSet();
adap.Fill(dats);
DataTable dt = new DataTable();
dt = dats.Tables[0];
ListItem lstitem = new ListItem();
lstitem = new ListItem();
lstitem.Text = "Select Service";
lstitem.Value = "s";
drpservice.Items.Add(lstitem);
foreach (DataRow datR in dt.Rows)
{
lstitem = new ListItem();
lstitem.Text = datR["Service"].ToString();
lstitem.Value = datR["Service"].ToString();
drpservice.Items.Add(lstitem);

}
con.Close();
}//end try
catch (Exception ex)
{

}
}

protected void Button1_Click(object sender, EventArgs e)
{
string name = (string)Session["Uname"];
name=name.Replace(".", " ");
name=name.ToUpper();
ServiceRestart(drpservice.SelectedValue.ToString());
//string connectionString = "";
string connectionString = "Data Source=172.27.4.227;Initial Catalog=ATMrestartservice;User ID=sa;Password=Wema123";
//string connectionString = "Data Source=172.27.4.227;Initial Catalog=ATMrestartservice;User ID=sa";
using (SqlConnection connection = new SqlConnection(connectionString))
{
SqlCommand cmd = new SqlCommand();

cmd.CommandText = "INSERT INTO Details ([Login Name],Date,[Service started]) VALUES ('" + name + "' ,'" + DateTime.Now + "', '" + drpservice.Text + "')";



cmd.CommandType = CommandType.Text;
cmd.Connection = connection;

connection.Open();
cmd.ExecuteNonQuery();
}

}


}
}
SuggestionRe: Windows services Not starting with c# codes Pin
Richard Deeming8-Jul-14 1:40
mveRichard Deeming8-Jul-14 1:40 
QuestionSession Timeout Pin
Member 106416987-Jul-14 19:57
MemberMember 106416987-Jul-14 19:57 
AnswerRe: Session Timeout Pin
Bernhard Hiller7-Jul-14 20:46
MemberBernhard Hiller7-Jul-14 20:46 
AnswerRe: Session Timeout Pin
Karen Mitchelle7-Jul-14 20:55
professionalKaren Mitchelle7-Jul-14 20:55 
GeneralRe: Session Timeout Pin
Richard MacCutchan7-Jul-14 22:22
mveRichard MacCutchan7-Jul-14 22:22 
AnswerRe: Session Timeout Pin
Richard MacCutchan7-Jul-14 22:23
mveRichard MacCutchan7-Jul-14 22:23 
SuggestionRe: Session Timeout Pin
ZurdoDev9-Jul-14 2:19
professionalZurdoDev9-Jul-14 2:19 
QuestionTexbox OnKeyPress event thru javascript executes a button click event Pin
bjay tiamsic7-Jul-14 15:37
Memberbjay tiamsic7-Jul-14 15:37 
AnswerRe: Texbox OnKeyPress event thru javascript executes a button click event Pin
Dave Kreskowiak7-Jul-14 17:46
mveDave Kreskowiak7-Jul-14 17:46 
QuestionAn MVC concern Pin
Neo101017-Jul-14 10:25
MemberNeo101017-Jul-14 10:25 
QuestionRe: An MVC concern Pin
Richard Deeming7-Jul-14 11:33
mveRichard Deeming7-Jul-14 11:33 
AnswerRe: An MVC concern Pin
Neo101017-Jul-14 22:33
MemberNeo101017-Jul-14 22:33 
AnswerRe: An MVC concern Pin
Richard MacCutchan7-Jul-14 22:21
mveRichard MacCutchan7-Jul-14 22:21 
QuestionHow to make Setup Project Pin
Amr Muhammed7-Jul-14 8:32
MemberAmr Muhammed7-Jul-14 8:32 
AnswerRe: How to make Setup Project Pin
OriginalGriff7-Jul-14 8:58
mveOriginalGriff7-Jul-14 8:58 
GeneralRe: How to make Setup Project Pin
Amr Muhammed7-Jul-14 11:30
MemberAmr Muhammed7-Jul-14 11:30 
GeneralRe: How to make Setup Project Pin
OriginalGriff7-Jul-14 22:25
mveOriginalGriff7-Jul-14 22:25 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.