473,411 Members | 2,154 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Use of unassigned local variable 'strSql' Error Message

126 100+
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

✓ answered by Joseph Martell

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.

3 2476
Plater
7,872 Expert 4TB
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 Expert 128KB
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 100+
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
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,...
3
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...
2
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...
9
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
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 =...
22
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
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
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...
2
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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,...
0
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,...
0
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...
0
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...
0
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...

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.