Archive for category C#

Finding power source of laptop in c#

Here i am going to write a simple process about how to detect whether your laptop running on battery or current. For this i create a simple windows form application.

A button is placed on the form. I just want to show the system so nothing added without this.

PowerSourceForm

Double click on the button and on its click event write these codes:

private void button1_Click(object sender, EventArgs e)
{

PowerLineStatus status = SystemInformation.PowerStatus.PowerLineStatus;

if (status == PowerLineStatus.Offline)
{
MessageBox.Show(“Running on Battery”);
}
else
{
MessageBox.Show(“Running on Power”);
}

}

PowerSource

Its done. So simple. Try this.

Fun with code.

1 Comment

Unable to update the EntitySet ‘yourTableName’ because it has a DefiningQuery and no element exists in the element to support the current operation.

Recently i am switching to MVC4 with Razor view engine for ASP.NET, C# web application.  Tomorrow i was developing a blogging site where there were some works related to database. There i have created a table with 4 columns but none of them were made primary key.

When i tried to run the application the visual studio 2012 shows the error “Unable to update the EntitySet ‘UserTable’ because it has a DefiningQuery and no <InsertFunction> element exists in the <ModificationFunctionMapping> element to support the current operation.” Here UserTable was my table name.

The database was sql server compact edition of visual studio 2012. I became confused about the error because this type of table with no primary key works well before. After a while i came to know that the problem was because of the lacking of primary key in the table. when i mark one of the columns as a primary key then the project run without error. This type of error was new to me so i thought to write down here so later on it may be helpful to someone like me.

Summary of the error and solution:

Problem: No primary key in the table

Solution: Add a primary key in the table

 

 

Happy Coding……….

Bikash, Software Engineer.

, , , , , , , ,

Leave a comment

Errors in Visual Studio During Application Development in C#

Error01:  Unable to find manifest sigining certificate in the certification store.

Solution01:

To get rid of from this error right click on the project name in the solution explorer (not in solution) and click in the properties. Then go to the Signing.

Then uncheck the checkbox>> Sign the ClickOnce manifests. Its done.

Solution02:

Click on the Button Select from Store.  A box will pop up name Windows Security. Click  ok. Its done.

Now your program will run without error.

Enjoy C#.

, , , , ,

2 Comments

Some of my projects

Touchless writer:

English to Bengali Dictionary:

PC to PC file transfer using socket :

, ,

Leave a comment

Connecting C# windows form application with MSSQL using ADO.NET

When you work with database application it is always need to connect your application with database like MSSQL or ORACLE or any others.
Now i am going to show you how to connect your application with MSSQL express edition using ADO.NET.
At first create a winform project. Then go the View menu and click Server Explorer. Right click on the label named Data Connection and click on Add Connection.

A new window will open. Select Microsoft SQL Server and click continue.

Then another new window will open. Write your server name on the first red rectangle area. Server name will be yourPCname\sqlexpress. Here my pc name is bikashpc. You can get your server name by clicking on the Servers node in Server Explorer window. if there is only one server then there is no confusion.

Then give a name to your database on the second red rectangle area. Here i give my database name bksdata. And then click ok.

Your application is now connected with database. Lets go test it.
Lets create a form like this >>

The task is you will put your friend roll number in the text box and by clicking Show Name button you will get the name of that friend in the name text box. So lets create a table for storing  friends name and roll number.
To create a table go the the Server Explorer and  click on the + at left side of your newly created database. It will expand and many others node will be shown including Tables,Views and so many. Right click on the table and click Add New Table.

Then a new tab will open in which you have to put your column name and data type. Write and save table by right clicking like on the tab like this >>

Here i add two column name and roll and name my table student. Lets put some data manually in the table. To do that click the + node of the lest side of the table. You can see you table here. Right click on your table name and click Show Table Data.

Add some name and roll.

Sometimes you need to change your table definitions:changing column name,adding column, changing data type and so on. To do that again Right click on your table name and click Open Table Definition.

Then modify what you want.
Lets write some program now for showing data into the text box. Double click on the button Show Name. In the code file it will look like this>>

 private void button1_Click(object sender, EventArgs e)
{

}
Add namespace for sqlclient on the top of the code file writing the code below>>

using System.Data.SqlClient;

Now you have to connect your application by writing some code. In the button action write

private void button1_Click(object sender, EventArgs e)
{
string connectionString = @”Data Source=bikashpc\sqlexpress;Initial Catalog=bksdb;     

                                                            Integrated Security=True”;
SqlConnection sqlCon = new SqlConnection(connectionString);

}

Your connection is complete.
Question is how can you get your connection string. Lets find out your Connectionstring. Again right click in your created database and click on the  Properties >>

Properties window will open.

Copy your Data Source and paste it into the red colored portion into connectionString. So you can now go to fetch data form your database. To do that you have to open your your connection by writing
>>sqlCon.Open();
Remember each time you open your connection you have to close it by writing
>>sqlCon.Close();

So lets add some code to your button action.


private void button1_Click(object sender, EventArgs e)
{
string connectionString = @”Data Source=bikashpc\sqlexpress;Initial Catalog=bksdb;    

                                                       Integrated Security=True”;
SqlConnection sqlCon = new SqlConnection(connectionString);

sqlCon.Open();
string commandString = “select name from student where roll='” + textBox1.Text + “‘”;
SqlCommand sqlCmd = new SqlCommand(commandString, sqlCon);
SqlDataReader read = sqlCmd.ExecuteReader();

while (read.Read())
{
textBox2.Text = read[“name”].ToString(); // it will show your friend’s name
}
sqlCon.Close();
}

Colored portion is used to fetch data from you database and show it in the textbox. Run your program and write a roll number in the textbox and then click the Show Name button. Wow!!!! what you see your friend’s name is shown in the name textbox.

This is all to inform you. I think it will help you a lot.

Have fun with C#.

, , , , , , , , ,

Leave a comment

Running a .exe file from windows form application using C#

Running exe file from C# windows form application is very easy.
Let You want to start bks.exe file when you click on the Luanch exe button like below:

Double click on the button. It will appear like below in the code file:

private void button1_Click(object sender, EventArgs e)
{

}

let you have exe fiel name bks.exe in the C drive. then you have to write
System.Diagnostics.Process.Start(@”C:\bks.exe”); in the button actions.

Then the total code will be look like:

private void button1_Click(object sender, EventArgs e)
{
System.Diagnostics.Process.Start(@”C:\bks.exe”);
}
Run your program and click the Lunch button. Wow!!! you have just created a program which can start another program from your pc.
Have fun with C#.

, ,

3 Comments