Hi,

I have a small application that reads an access database. I'm able to load the data, but it's in ascii. I created a function that does the conversion but for some reason I can't figure out how to implement it correctly. Where do I call the AsciiToString() function? Here is my code, thanks so much.

aspx

<asp:DropDownList ID="mydropdownList" runat="server" />

aspx.cs

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
           
            LoadSearchList();

        }
    }


    private void LoadSearchList()
    {
        //Declare objects
        OleDbConnection conn;
        OleDbCommand comm;
        OleDbDataReader reader;
        //Read the connection string from Web.config
        string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;
        // Initialize connection
        conn = new OleDbConnection(connectionString);      
        

        // Create command
        comm = new OleDbCommand("SELECT myID, title FROM books", conn);
       
        //Enclose database code in Try-Catch-Finally
        try
        {
            //Open the connection
            conn.Open();
            //Execute the command
            reader = comm.ExecuteReader();
            while (reader.Read())
            {
                     
               
                //Populate the list of categories            
                mydropdownList.DataSource = reader;               
                mydropdownList.DataValueField = "myID";
                mydropdownList.DataTextField = "title";
                mydropdownList.DataBind();
                               
            }
                                
       
            //close the reader
            reader.Close();
        }
        catch
        {
           
        }

        finally
        {
            //Close the connection
            conn.Close();
        }

             
    }



    private static string AsciiToString(string content)
    {
        StringBuilder sbTemp = new StringBuilder(content.Length);
        string StrValue = "";
        while (content.Length > 0)
        {

           
            StrValue += System.Convert.ToChar(System.Convert.ToInt32(content.Substring(0, 3))).ToString();
           
            content = content.Substring(3, content.Length - 3);

           
        }
        content = StrValue;
        return content;

    }

Recommended Answers

All 4 Replies

@lm111

For easy readability, always wrap programming code within posts in CODE-tags

Use DataAdapter.

protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            LoadSearchList();
    }

private void LoadSearchList()
{

        string connectionString = ConfigurationManager.ConnectionStrings["ApplicationServices"].ConnectionString;

        OleDbDataAdapter adp=new OleDbDataAdapter("SELECT myID, title FROM books",connectionString);

        DataTable dt=new DataTable();

        adp.Fill(dt);       

        mydropdownList.DataSource = dt
        mydropdownList.DataValueField = "myID";
        mydropdownList.DataTextField = "title";
        mydropdownList.DataBind();
}

Thank you.

Could someone please help me convert the following function to c#? Thanks.

Function StringToAscii(str)
	Dim result, x
	StringToAscii = ""
	If Len(str)=0 Then Exit Function
	If Len(str)=1 Then
		result = Asc(Mid(str, 1, 1))
		StringToAscii = Left("000", 3-Len(CStr(result))) & CStr(result)
		Exit Function
	End If
	result = ""
	For x=1 To Len(str)
		result = result & StringToAscii(Mid(str, x, 1))
	Next
	StringToAscii = result
End Function

@lm111. You should have to create a new thread to ask different question. Anyway take a look at this code snippet.

public static string  StringToAscii(string  str)
    {
        string   result = null;
            int  x=0;
        
        if (str.Length == 0)
            return "";
        if (str.Length == 1)
        {
            result = str.Substring(0, 1);
            return new string('0', 2 - result.Length) +  (int)result[0];
            
        }
        result = "";
        for (x = 0; x < str.Length ; x++)
        {
            result = result + StringToAscii(str.Substring(x, 1));
        }
        return result;
    }

PS: If you post a question and it gets resolved, please use the Mark Solved link to mark your thread solved.

Thanks, adatapost!!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.