In this article, we will see all the steps for creating an AWS Free Tier account (for one year) and we will create an Amazon DynamoDB table from a .NET WinForms application. Later, we will insert some records to this DynamoDB table and display all the records in a Data Grid View control.
Step 1 - Create a free AWS account for one year.
AWS Marketplace offers free and paid software products that run on the AWS Free Tier. If you qualify for the AWS Free Tier, you can use many products.
Please visit this URL to create a free account.
Please give your email address and choose a password for your AWS account. Also, give a valid AWS account name. Click the “Continue” button to proceed. Give your contact information. Please give a valid phone number because it will be validating in the debit/credit card validation time.
Here, you must give the Credit/Debit card information. Please give the valid information.
You will be charged Rs. 2 from your bank account for this validation process. After some days, this amount will be credited to your bank account.
Please provide your phone number. You will get a call to this number for validating your account.
You can choose a plan from the below screen. Opt for the free plan. If needed, you can choose other plans also.
The next screen will be personalized to your experience. Please choose the suitable details.
Our Sign-Up process will be completed now.
You can click the “Complete Sign Up” button.
If you log on to your account, you will be notified with the below message. You can wait for some more time and your account will be activated shortly.
After some time, you will get an email to your registered email address from Amazon.
Congratulations, your AWS free tier account is now activated.
You can sign into your account console now.
You can see all the services available with AWS. Please note that all these services are not free. But you can use many free services.
Currently, there are 15 geographic locations available in AWS. Please choose any of these locations. I chose Asia Pacific (Mumbai) location.
Step 2 - Please create Access Keys.
We must create access keys for connecting any of the AWS services from outside AWS. We are going to create an Amazon DynamoDB from the .NET application we need the access key to do this.
Please click “My Security Credentials” option under your username.
It will open another screen. Please expand the “Access keys”.
Please click “Create New Access Key” button. It will create a new access key for you. Often, you should change your access keys for security reasons.
Your access key is created now. You can download the access key and secret key. Please save these keys carefully. We will use these keys in our .NET application soon.
Step 3 - Create a .NET WinForms application to create DynamoDB
We are going to create a WinForms application. We will create a Student model with Student Id, Student Name, College Name, and Class Name. We will use this application to insert the Student data to DynamoDB and display Student records in a Grid. I am using Visual Studio 2017 to create a WinForms Application.
Please add “AWSSDK.DynamoDBv2” NuGet Package to your WinForms application. This library is used to create DynamoDB database and used for all CRUD actions.
We can add two buttons to our main form - one button for inserting the student record and another button for displaying the existing records from DynamoDB.
I have added a panel control and added three text boxes to get user input for Student data. Please note that Student Id will be created automatically (We use GUID for this).
We can add one more panel control and add a data grid view control to display our student data.
Our design part is over now.
We can add a Student model class now. Please create a “Models” folder and create Student class inside this folder.
Student.cs
- using Amazon.DynamoDBv2.DataModel;
-
- namespace AmazonDynamoDB.Models
- {
- [DynamoDBTable("Student")]
- public class Student
- {
- public string studentId { get; set; }
- public string studentName { get; set; }
- public string collegeName { get; set; }
- public string className { get; set; }
- public int isActive { get; set; }
- }
- }
I have imported “Amazon.DynamoDBv2.DataModel” library in this class.
We can add a “CreateTable” method now. We will call this method inside the Form_Load event.
CreateTablemethod
- private void CreateTable()
- {
- var credentials = new BasicAWSCredentials(accessKey, secretKey);
- client = new AmazonDynamoDBClient(credentials, RegionEndpoint.APSouth1);
-
- var tableResponse = client.ListTables();
- if (!tableResponse.TableNames.Contains(tableName))
- {
- MessageBox.Show("Table not found, creating table => " + tableName);
- client.CreateTable(new CreateTableRequest
- {
- TableName = tableName,
- ProvisionedThroughput = new ProvisionedThroughput
- {
- ReadCapacityUnits = 3,
- WriteCapacityUnits = 1
- },
- KeySchema = new List<KeySchemaElement>
- {
- new KeySchemaElement
- {
- AttributeName = hashKey,
- KeyType = KeyType.HASH
- }
- },
- AttributeDefinitions = new List<AttributeDefinition>
- {
- new AttributeDefinition { AttributeName = hashKey, AttributeType=ScalarAttributeType.S }
- }
- });
-
- bool isTableAvailable = false;
- while (!isTableAvailable)
- {
- Console.WriteLine("Waiting for table to be active...");
- Thread.Sleep(5000);
- var tableStatus = client.DescribeTable(tableName);
- isTableAvailable = tableStatus.Table.TableStatus == "ACTIVE";
- }
- MessageBox.Show("DynamoDB Table Created Successfully!");
- }
- }
BtnInsert_Click event
- private void BtnInsert_Click(object sender, EventArgs e)
- {
- TxtStudentName.Text = string.Empty;
- TxtCollegeName.Text = string.Empty;
- TxtClassName.Text = string.Empty;
- PnlInsert.Visible = true;
- }
BtnSave_Click event
- private void BtnSave_Click(object sender, EventArgs e)
- {
-
- context = new DynamoDBContext(client);
-
-
- Student currentState = new Student
- {
- studentId = Guid.NewGuid().ToString(),
- studentName = TxtStudentName.Text,
- collegeName = TxtCollegeName.Text,
- className = TxtClassName.Text,
- isActive = 1
- };
-
-
- context.Save<Student>(currentState);
-
- MessageBox.Show("Student Record Inserted Successfully!");
-
- PnlInsert.Visible = false;
- }
BtnDisplay_Click event
- private void BtnDisplay_Click(object sender, EventArgs e)
- {
-
- context = new DynamoDBContext(client);
-
- Table StudentTable = Table.LoadTable(client, tableName);
-
- ScanFilter scanFilter = new ScanFilter();
- scanFilter.AddCondition("isActive", ScanOperator.Equal, 1);
-
- Search search = StudentTable.Scan(scanFilter);
-
- List<Document> documentList = new List<Document>();
-
- DGV.Rows.Clear();
- DGV.ColumnCount = 4;
-
- DGV.Columns[0].Width = 270;
-
- DataGridViewRow row = new DataGridViewRow();
- row.CreateCells(DGV);
- row.Cells[0].Value = "Student Id";
- row.Cells[1].Value = "Student Name";
- row.Cells[2].Value = "College Name";
- row.Cells[3].Value = "Class Name";
- row.DefaultCellStyle.BackColor = Color.Blue;
- row.DefaultCellStyle.ForeColor = Color.White;
- DGV.Rows.Add(row);
- do
- {
- documentList = search.GetNextSet();
- foreach (var document in documentList)
- {
- row = new DataGridViewRow();
- row.CreateCells(DGV);
- foreach (var attribute in document.GetAttributeNames())
- {
- string stringValue = null;
- var value = document[attribute];
- if (value is Primitive)
- stringValue = value.AsPrimitive().Value.ToString();
- else if (value is PrimitiveList)
- stringValue = string.Join(",", (from primitive
- in value.AsPrimitiveList().Entries
-
- select primitive.Value).ToArray());
- if (attribute == "studentId")
- {
- row.Cells[0].Value = stringValue;
- }
- else if (attribute == "studentName")
- {
- row.Cells[1].Value = stringValue;
- }
- else if (attribute == "collegeName")
- {
- row.Cells[2].Value = stringValue;
- }
- else if (attribute == "className")
- {
- row.Cells[3].Value = stringValue;
- }
- }
- DGV.Rows.Add(row);
-
-
- }
- } while (!search.IsDone);
-
- foreach (DataGridViewColumn c in DGV.Columns)
- {
- c.DefaultCellStyle.Font = new Font("Arial", 12F, GraphicsUnit.Pixel);
- }
-
- PnlGrid.Visible = true;
- }
We have completed all the event methods inside our form. We can run our application.
We have called the DynamoDB Table creation method inside the Form Load event. Our new DynamoDB table will be created shortly.
We can insert a Student record now.
After clicking the “Save” button, our new Student record will be inserted to the DynamoDB table.
We can verify this data in AWS console. Please come to AWS console and open DynamoDB service.
You can see the newly created Student table there. We have used a “studentid” key as a partition key. This partition key can be used for all the searching operations.
If you click on the "Student", it will open the table details. Please click the “Items” tab. It will show all the records. Currently, there is only one record inside the table.
If you double-click the studentid, it will open entire row information.
We can insert one more student record from our WinForms application. Now, we can display these two records by clicking “Display Students” button.
In this article, we saw the steps for creating a free AWS account (one year) and after that, we created a DynamoDB table from our WinForms application. We successfully inserted two student records to DynamoDB table and we displayed those records in a Data Grid View control also. In my upcoming articles, we can see entire CRUD actions with DynamoDB and see more AWS services.
Robert WagonerPosted Jan 11, 2021, 5:33 PM
Sorry, I meant to comment in your Blazor version that isn't working.
Robert WagonerPosted Jan 11, 2021, 5:32 PM
Well done article, however the code doesn't work for VSC 2019. Can you update please.
DeepanPosted Jul 9, 2019, 2:01 AM
Useful article..