Click here to Skip to main content
15,895,746 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
C#
public string UpdateUserID(string updateID)
    {
        UpdatedBy = System.Web.HttpContext.Current.Request.LogonUserIdentity.Name.Substring(12);

        string sql = "select Update_UserID from Cwipuser.CWIP_Variances  ";
        SqlCommand cmd = new SqlCommand(sql, MyCon);
        MyCon.Open();
        SqlDataReader rdr = cmd.ExecuteReader();

        while (rdr.Read())
        {
          if (!rdr.IsDBNull(1))
            {
                updateID = rdr.GetString(1);
            }
              updateID = UpdatedBy;
        }
        MyCon.Close();
        return updateID;
    }
Posted
Updated 27-Aug-14 1:19am
v3
Comments
[no name] 27-Aug-14 7:14am    
You are only selecting 1 column and trying to access the 2nd element.
mampaf 27-Aug-14 7:17am    
yes because i want to get the second value from the DB
[no name] 27-Aug-14 7:18am    
You don't understand how arrays work do you?
ClimerChinna 27-Aug-14 7:19am    
i think the result is having only one value, check it by debugging
mampaf 27-Aug-14 7:26am    
ok thanks i will debug it,and if it stil doesnt work u will help me out right. . ?

try to give like below

SQL
while (rdr.Read())
{
if (!rdr.IsDBNull(0))
{
updateID = rdr.GetString(0);
}
updateID = UpdatedBy;
}
 
Share this answer
 
Comments
mampaf 27-Aug-14 7:24am    
that one is for "ID" and i want "updateID" which is at position1
ClimerChinna 27-Aug-14 7:27am    
what is id and updateid here you are returning only updateuserid from the select query right then how will you get id
mampaf 27-Aug-14 7:32am    
ID is just an auto generated pk frm the DB,UpdateID is the user ID of the Person who updated a coment on the web page.
ClimerChinna 27-Aug-14 7:36am    
but in your select query you are returning only update_Userid right? then how will you get ID
mampaf 27-Aug-14 7:50am    
i dont want ID but updatedID thats y m returning it, i wna take Update_UserID frm the DB and asign it updatedID and UpdatedBy on the web page acording to the person hu loged in
You're returning only one field Update_UserID from the query...therefore you have to access index 0 not 1.

That is updateID = rdr.GetString(0)

Same for IsNull function.


Alternatively, IndexOutOfRangeException could be thrown by Substring(12) if UserIdentity.Name is shorter then 13 characters (again, 0-based index means you're starting from 13th character)
 
Share this answer
 
v2
Comments
mampaf 27-Aug-14 7:20am    
at (0) there is something that i dont want thats y i am saying (1)
mampaf 27-Aug-14 7:30am    
oooh ya i think it myt be the substring,so should i increase the 12 to 13 or what ?
mampaf 27-Aug-14 8:19am    
doesnt work
Sinisa Hajnal 28-Aug-14 2:17am    
Depends on why do you do it...If you're just trying to remove domain name or computer name from the username, better use split function. Otherwise, find (via breakpoint) what data comes there and either don't substring or find a function that fits (i.e. yourstring.substring(yourstring.length -3) will take only last 3 characters, yourstring.split("\")(1) will take domain\username and select only username etc...

Also, INCREASING from 12 to 13 cannot work, already 12 is too large and you get out of range exception, you should LOWER it (to 11 if you want to ignore first 11 characters and take 12th)
mampaf 28-Aug-14 2:45am    
thnx i got it, hey is it posible to use a foreach row in datagrid.rows but only for one row?like it should not loop for the whole 5rows,it shud only loop once n stop

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