473,549 Members | 2,346 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Use of unassigned local variable 'strSql' Error Message

126 New Member
Hi Good Guy,

I am very surprised of the coding generate error message
Error 1 Use of unassigned local variable 'strSql'

even though the strSQL variable was declared. Not sure what is wrong with the overall coding;


Here are the coding:

Expand|Select|Wrap|Line Numbers
  1.  private void FLoadDataGridView()
  2.     {
  3.        string strSql;
  4.  
  5.        if (this.radioBtnAll.Checked == true)
  6.        {
  7.        strSql = "Select * from Customers Order by CustomerID";
  8.        }
  9.  
  10.       if (this.radioBtnSome.Checked == true)
  11.        {
  12.        strSql = "Select * from Customers ";
  13.        strSql += "Where CustomerID like 'this.txtSome.Text%'";
  14.        strSql += "Order by CustomerID ";
  15.       }
  16.  
  17.        sqlConn = new SqlConnection(connStr);
  18.        sqlConn.Open();
  19.        DA = new SqlDataAdapter(strSql, sqlConn);  <---- Error           
  20.        DS = new DataSet("DS");            
  21.        DA.Fill(DS,"Customer");
  22.  
  23.       dataGridView1.DataSource = DS.Tables["Customer"];
  24.       dataGridView1.ClearSelection();
  25.  
  26.        sqlConn.Close();
  27.        DS.Dispose();            
  28.  
  29. }

Thank You.

Cheers,
Lennie
Jun 22 '10 #1
3 2490
Plater
7,872 Recognized Expert Expert
You never set it to anything unless one of the radio buttons is checked, but always try to use it. You should probably only do the Sql work if the button is checked?
Jun 22 '10 #2
Joseph Martell
198 Recognized Expert New Member
The error has nothing to do with the fact that your variable has been declared or not. It is in reference to the fact that it is possible for you to get to the line

Expand|Select|Wrap|Line Numbers
  1. DA = new SqlDataAdapter(strSql, sqlConn);
Without assigning a value to strSql and therefor strSql could be null.

This error can be avoided by declaring strSql as:

Expand|Select|Wrap|Line Numbers
  1. string strSql = "";
but that only hides the problem. Even initializing strSql to "" would still generate an error if you try to use it. You need to have some sort of default case. Assuming that only one of the radio buttons (radioBtnAll and radioBtnSome) can be checked and 1 MUST be checked, then you could do something like this, using radioBtnAll as your default case:

Expand|Select|Wrap|Line Numbers
  1. if (radioBtnSome.Checked)
  2. {
  3.     strSql = "Select * from Customers ";
  4.     strSql += "Where CustomerID like 'this.txtSome.Text%'";
  5.     strSql += "Order by CustomerID ";
  6. }
  7. else
  8. {
  9.     strSql = "Select * from Customers Order by CustomerID";
  10. }
This would guarantee initialization of strSql before use (even if a new radio button was added later on) and gets rid of the warning.
Jun 22 '10 #3
lenniekuah
126 New Member
Hullo Plater and IBM1313,

Thanks to both of you for sharing your sample coding and advices. I have followed your advices and coding and now my coding is running very well.

Both of you are just awesome and so glad to meet both of you here. This FORUM is awesome with good helpers of you 2 here.
Jun 23 '10 #4

Sign in to post your reply or Sign up for a free account.

Similar topics

5
1337
by: Mike P | last post by:
I am instantiating a class in a switch statement as there are a number of different overloads depending upon the data entered by the user. The problem I have is that after instantiating my class, when I try to call methods in the class later on in my code using the same object I cannot, I get the error 'use of unassigned local variable'. ...
3
6462
by: John Smith | last post by:
In the following (pseudo)code, why is it that C# returns an "unassigned local variable" error for nVar? It seems I have to declare the variable outside foo() for the error to dissapear. void foo() { int nVar = 0; SqlDataReader rdr = objCmd.ExecuteReader();
2
3782
by: Doug | last post by:
I am trying to work with an object and create it like so: EXTRA.Sessions oSessions = new EXTRA.Sessions(); This doesn't work because I get this error: 'Cannot create an instance of the abstract class or interface 'EXTRA.Sessions'.' So I try to do this:
9
6178
by: tshad | last post by:
I am getting an error: Use of unassigned local variable 'postDateRow' But it is assigned. Here is the code: int payDateRow; int postDateRow;
3
529
by: Antonio | last post by:
Can somebody tell me what's wrong with this code? When I try to debug, I get "Use of unassigned local variable 'ip2se'. string ip2se; if(e.Item.Cells.Text == e.Item.Cells.Text) ip2se = e.Item.Cells.Text.ToString(); Thanks,
22
8394
by: Laura T. | last post by:
In the following example, c# 2.0 compiler says that a3 and ret are used before assigned. as far as I can see, definite assignment is made. If I add finally { ret = true; a3 = "b3";
8
11137
by: Dom | last post by:
This is a little tricky, but I think the error I'm getting isn't valid. The compiler just doesn't know my logic. MyObject o; switch (IntVariable) { case 1: o = new MyObject() break;
3
3851
by: Hegel | last post by:
Some body please help me. I finds this code really correct. However I am a beginner The following code yields Use of unassigned local variable 'fout' error in my pgm please help using System; using System.IO; using System.Collections.Generic;
2
6417
by: plasmay | last post by:
Hi, I am new in learning c#, and have recently encountered a problem: static float ComputeAvg(float a) { float sum; int i; for (i = 0; i <= a.Length; i++) { sum = sum + a;
0
7546
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7471
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
7740
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
1
7503
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7830
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
0
6071
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5387
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5111
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3496
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.