Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Dear All,

i know this is basic question but i am new in .net so need help to expert .
My query is i want to read null value from table if max value is null i am set one hardcode if get mas value 2 or 3 or 4 tha increamen var++ but i am not bale to do pls help me

C#
var ScriptId = "null";
                string sql = "Select Max(ScriptId) from tbl_Script";
                SqlCommand cmd = new SqlCommand(sql, connection);
                using (var reader = cmd.ExecuteReader())
                {
                    if (reader["ScriptId"] == System.DBNull.Value)
                    {
                        ScriptId += 1;
                    }
                    else
                    {
                        ScriptId = ScriptId++;
                    }
Posted

1 solution

Please explain you problem instead of just saying you not able to do. What error you getting. Please take care of the typos in your question.

Anyway, in your code, you are assigning "null" as string to ScriptId variable. Which will make the variable of type string. So, you can not increment string value. In the code your doing same in if and else condition.
 
Share this answer
 
Comments
Arjunwalmiki 14-Feb-14 1:37am    
Yes Vijay you r right
Anyway, in your code, you are assigning "null" as string to ScriptId variable. Which will make the variable of type string. So, you can not increment string value. In the code your doing same in if and else condition.

i want to do same thing actully in my table value is null if null than var ScriptId =1;
else Iif i am get one value from table) ScriptId ++;
Arjunwalmiki 14-Feb-14 2:21am    
i am change
var ScriptId = 0;
know received error is in code line if (reader["ScriptId"] == System.DBNull.Value)
indexoutofrangeexception was unhandled

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900