Converting Types in C#

I am not a great software developer. I’m OK, and I do know how to use Google and Stack Overflow well. Maybe my best skill is wording searches well? In any case, I’ve had to write a bit of C# lately to build an app for my Zero Downtime talk.

In no way am I an expert on this stuff, but I learned a few things while working on the client. One of these was how to get to/from various datatypes. Not a complex skill, but since I do it rarely, I decided to do a quick blog.

Creating Strings

I actually remembered how to take a number and convert it to a string. The ToString() method works on many  items. In my case, I used some variables as numerics, often a zero or one, but I wanted to display these in a textbox. To do that, I needed something like this:

txtRandom.Text = num.ToString();

Easy enough to write, but other items were more complex.

Strings to Int

I only had to go the reverse way a few times, but found there was no ToInt32() or similar. I was hoping for one, but a little google query helped me realize this is what I needed?

cmd.Parameters.Add("@year", SqlDbType.Int).Value = Convert.ToInt32(txtYear.Text);

In this case, I was adding an integer parameter from a value in a textbox, which was a strong. The Convert class has a ToInt32() method I used.

String to Date

In one demo I move to a date as a parameter, again getting a value from a string textbox. This was yet another method. In this case, there is a DateTime class with a Parse() method. I used it as such:

cmd.Parameters.Add("@start", SqlDbType.DateTime).Value = DateTime.Parse(txtStart.Text);

This worked great.

NULLs

One other item I needed was passing a NULL value to a proc. Part of zero downtime deployments involve staging your changes, and that sometimes means altering which parameters you use and sending in other values. In this case, I found a DBNull class with a value property. I passed null parameter values like this:

cmd.Parameters.Add("@year", SqlDbType.Int).Value = DBNull.Value;

More

There are obviously other conversions, but I’ll learn those as I need them. I know how to phrase a quick question and read through StackOverflow to find code I need and modify it. I’m good at asking questions and listening.

If you have advice, please leave me a comment. I had some fun doing this, and I’m glad I didn’t need to bother too many people to get this working.

Posted in Blog | Tagged , , | 2 Comments

Daily Coping 22 Mar 2023

Today’s coping tip is to appreciate your hands and all the things they do for you.

I make a living with my hands. I type constantly, which is a big part of the work I do at Redgate. When I have a cut on a finger, as I did recently, it’s hard to type. That makes me much less productive, and lengthens my workday or workweek.

I also use my hands in job #2. I coach kids in volleyball and I’m regularly hitting balls in the air, which becomes painful or less controlled if my hands have issues. Or my shoulder, but that’s another post. I also take stats on paper and type totals into a spreadsheet, so yeah, hands are important.

Around the house, hands help me fix things on the ranch or play guitar.

I really appreciate my health and ability to use my hands to make life better.

I started to add a daily coping tip to the SQL Server Central newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 22 Mar 2023

Daily Coping 21 Mar 2023

Today’s coping tip is to listen to a piece of music without doing anything else.

This is surprisingly hard. I turn something on, but then I look around, think of something else, walk, exercise, drive, etc.

Travel is a good way to do this, and I’ve learned sometimes that I can take a few minutes to unwind by staring at the world and listening to something in headphones. Hard for a multitasker like me, but it can be refreshing. If I’m not too busy.

For a trip last week, I got to the airport a bit early. I knew I’d eat on the plane, so I went to the lounge, got a glass of wine, and sat facing a window. I put on a little John Mayer and then sat there just listening.

Lately a couple songs have been running through my head, so I ran Hey Marie and Slow Dancing in a Burning Room together, just listening. Well, listening and sipping chardonnay.

I started to add a daily coping tip to the SQL Server Central newsletter and to the Community Circle, which is helping me deal with the issues in the world. I’m adding my responses for each day here. All my coping tips are under this tag.

Posted in Blog | Tagged , , | Comments Off on Daily Coping 21 Mar 2023

Viva Las Vegas–Off to VS Live

I’m off to VS Live Las Vegas  for a few days this week. I was in the UK last week and I leave early Wed-Fri for a trip this week.

If you’ve never been to VS Live, it’s a neat event. Smaller than many conferences, but it covers a bunch of different technologies for developers. Databases are a small part, and I’m always honored when I get to speak there. I hear different perspectives and questions than I find at many data professional events.

I also learn some interesting things about software development from others. To me, this is a great way to round out my knowledge. When I was more of an attendee than a speaker, I liked going to these events that are focused on multiple technologies.

If you want a last minute trip to Vegas, come this week. If not, there are some other great events coming up this year. Think about going to one if you do a lot of development:

I won’t be at those, sadly, but my schedule is a little crazy in 2023, so I’m limiting the submissions. Hopefully I’ll do a few more of these next year.

Posted in Blog | Tagged , , | 2 Comments