Click here to Skip to main content
16,019,349 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
[WebMethod]
public void readOneModelImage(string id)
{
    try
    {
        String strJson;
        SqlConnection conn = new SqlConnection(ConfigurationManager.ConnectionStrings["myCS"].ConnectionString);
        string strSQL = "select * from PosData2018New.dbo.Client_Define where client_no = '" + id + "'";
        conn.Open();
        SqlCommand cmd = new SqlCommand(strSQL, conn);
        
        DataTable dt = new DataTable();
        dt.Load(cmd.ExecuteReader());
        conn.Close();
        
        OneModelImage[] m = new OneModelImage[dt.Rows.Count];
        int counter = 0;
        foreach (DataRow row in dt.Rows)
        {
            m[counter] = new OneModelImage();
            m[counter].MoNo = row.Field<string>("mod_no");
            m[counter].Mona = row.Field<string>("mod_name");
            m[counter].ImageUrl = row.Field<string>("ModelImage");
            var xx = row.Field<string>("ModelImage");
        
                counter++;
        }
        
        strJson = serializer.Serialize(m);
        Context.Response.ContentType = "application/json";
        Context.Response.ContentEncoding = Encoding.UTF8;
        Context.Response.Write(strJson);
    }
 

    catch (Exception ex)
    {
    }
}


What I have tried:

no solve and can not open new form
Posted
Updated 6-Sep-20 20:07pm
v2
Comments
Sandeep Mewara 7-Sep-20 1:16am    
It's not clear what are you trying to do. Your code is retrieving data from DB but your question states on click image does not open.
F-ES Sitecore 7-Sep-20 5:38am    
Remove the "catch" block, it's just hiding any exceptions that are being thrown. Use the debugger to check the json generated is what you expect, and if it is check how you use that json in the client browser.

1 solution

First off, don't do it like that! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?
And in a web app ... that's really not a good idea.

The rest of your problem you are going to have to think about - that code gets data (badly) from a DB, but that code doesn't even try to one images or forms so your whole question is meaningless.
 
Share this answer
 

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